Can anyone help me to get the estimate value of float value 1001.27124 to 1001.27? Math.round(float) function return the int value. But I want to get float value result with 2 decimal point; Thanks in advance.
perlsu
0
Newbie Poster
Recommended Answers
Jump to PostTry this code
double num = 1001.27124; double newNum = Math.round(num*100.0)/100.0; System.out.println (newNum); //prints 1001.27
It works because *100 shifts the numbers to the left twice. Then the round is applied. Then we divide by 100 to shift twice right.
Jump to Postimport java.util.*;
DecimalFormat dec = new DecimalFormat("###.##");
System.out.println(dec.format(value));
All 7 Replies
NPH
0
Junior Poster in Training
server_crash
64
Postaholic
jwenting
1,889
duckman
Team Colleague
cheenu78
0
Light Poster
NavinBhutada
0
Newbie Poster
tong1
22
Posting Whiz
mubin_89
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.