I have a number 10.625
In JavaScript
10.625.toFixed(2) //gives 10.63
In Java,
Double.valueOf(new DecimalFormat("###.##").format(Double.valueOf("10.625"))) //gives 10.62
How I can achieve unique output both at client and server side?
I have a number 10.625
In JavaScript
10.625.toFixed(2) //gives 10.63
In Java,
Double.valueOf(new DecimalFormat("###.##").format(Double.valueOf("10.625"))) //gives 10.62
How I can achieve unique output both at client and server side?
The way to harmonise the view of the number on client and server is to do your processing on the server and have the client display what the server sends it.
Use the Java version on the server, convert to a String
, and send that to the client. Then you'll just be doing the conversion once, and won't have the possibility of a conflict.
After all, the client is just supposed to be giving a nice view of what the server has done. Repeating the calculations on the client is not the right way to think about it.
BigDecimal bd = new BigDecimal("10.625");
bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);
double value = Double.parseDouble(new DecimalFormat(".##").format(bd));
System.out.println(value);
will also print 10.63
Some important infos to catch :
.
then you can simpy use .##
DecimalFormat
==> .##
format will print 0.x0
to 0.x
while '0.00
' format will print 0.x0
to 0.x0
so i prefer the second format