How can I format a String number to have commas and round?

You might want to look at the DecimalFormat class; it supports different locales (eg: in some countries that would get formatted as 1.000.500.000,57 instead). You also need to convert that string into a number, this can be done with: double amount = Double.parseDouble(number); Code sample: String number = “1000500000.574”; double amount = Double.parseDouble(number); DecimalFormat formatter … Read more

How to round a number to n decimal places in Java

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output. Example: DecimalFormat df = new DecimalFormat(“#.####”); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); } gives the output: 12 123.1235 0.23 0.1 2341234.2125 EDIT: … Read more

How To Represent 0.1 In Floating Point Arithmetic And Decimal

I’ve always pointed people towards Harald Schmidt’s online converter, along with the Wikipedia IEEE754-1985 article with its nice pictures. For those two specific values, you get (for 0.1): s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm 1/n 0 01111011 10011001100110011001101 | || || || || || +- 8388608 | || || || || |+— 2097152 | || || || || … Read more