Reputation: 11
i try to only 2 digit after decimal point without last 2 digit roundoff in java. Here my code :-
val df = DecimalFormat("#.##")
val gh = df.format(19.14966566)
ans of df is = 19.15
but i want only 19.14 as it is last 2 digit of 19.14966566 without roundoff
Upvotes: 0
Views: 745
Reputation: 164064
One more solution for you with NumberFormat
.
With NumberFormat
you can explicitly set:
Like this:
val nf = NumberFormat.getInstance(Locale.US)
nf.minimumFractionDigits = 2
nf.maximumFractionDigits = 2
nf.roundingMode = RoundingMode.DOWN
val gh = nf.format(19.14966566)
println(gh)
will print:
19.14
Upvotes: 0
Reputation: 2677
You can use below code
double x = 19.14966566;
double y = Math.floor(x * 100) / 100;
O/P-> 19.14
In your code style
val df = 19.14966566
val gh = Math.floor(x * 100) / 100
gh-> 19.14
You can refer this link also: https://stackoverflow.com/a/32303271/6838146
Let me know if you face any issue
Upvotes: 1
Reputation: 2889
You can try this...It works for me...
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.FLOOR
val gh = df.format(19.14966566)
Upvotes: 0