i have a double number which is "547.123456"
i just want to use this double as "547.1" like only 1 number after "."
How can i do that?
i have a double number which is "547.123456"
i just want to use this double as "547.1" like only 1 number after "."
How can i do that?
Use BigDecimal
double f=547.123456;
BigDecimal d=new BigDecimal(f);
System.out.print( d.setScale(1, RoundingMode.FLOOR));
If you want to trancate a value e.g.
47.12345 -> 47.1
47.56789 -> 47.5 // <- not 47.6!
you can do it via floor
double value = 47.123456;
double result = Math.floor(value * 10.0) / 10.0;
If you want to round a value e.g.
47.12345 -> 47.1
47.56789 -> 47.6 // <- not 47.5!
you can do it via round
double value = 47.123456;
double result = Math.round(value * 10.0) / 10.0;
Use a String.format
String.format("%.1f",547.123456);
This an easiest way i think.
Very easily :)
double x = 47.123456;
x = (long)(x*10)/(double)10
There is a general solution for every precision, you can specify your own like so:
DecimalFormat decimalFormat = new DecimalFormat("#.#");
System.out.println(decimalFormat.format(<your double>));