Formatting floating point values in Java -
this question has answer here:
i tried use this:
string t="4.99999999"; t = string.format("%.2f",float.valueof(t)); i wanted print 4.99, wrote 5.00. know why?
there 2 issues, here. 1 floats can't differentiate between 4.99999999 , 5.0, i.e. float.valueof() parses 5.0. doubles do have enough precision. other issue 5.0 correct behavior %2f. you're asking 2 decimal points, , 4.99999999 rounds 5.00. since string.format() behavior isn't want, want, i.e. why 4.99 correct behavior use case?
edit: if you're trying truncate 2 two decimal points, here: how can truncate double 2 decimal places in java
Comments
Post a Comment