Javascript toFixed equivalent in java

I am using toFixed (2) on the client side (javascript). But on the server side (java) I have to recount. How can i do this?

+6
source share
2 answers

setScale() BigDecimal will do this for you on the server side

 import java.math.BigDecimal; public class Main{ public static void main(String ar[]){ float number = 123.123456F; BigDecimal numberBigDecimal = new BigDecimal(number); System.out.println(numberBigDecimal); numberBigDecimal = numberBigDecimal .setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println(numberBigDecimal); } } 
+7
source

DecimalFormat format method - this is equivalent in java toFixed in javascript.

 DecimalFormat decimalFormat = new DecimalFormat("0.##"); System.out.println(decimalFormat.format(3.1000565)); 
+3
source

Source: https://habr.com/ru/post/915967/


All Articles