Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toFixed equivalent in java

I am using toFixed(2) in client side(javascript). But in the server side(java) i have to recalculate. how can i do this?

like image 795
jay Avatar asked May 17 '12 07:05

jay


1 Answers

setScale() of BigDecimal will do it for you on 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);
}
}
like image 163
jmj Avatar answered Sep 20 '22 03:09

jmj