How do you round a float to 2 decimal places in JRuby?
Solution 1
Float#round can take a parameter in Ruby 1.9, not in Ruby 1.8. JRuby defaults to 1.8, but it is capable of running in 1.9 mode.
Solution 2
(5.65235534).round(2)
#=> 5.65
Solution 3
sprintf('%.2f', number)
is a cryptic, but very powerful way of formatting numbers. The result is always a string, but since you're rounding I assume you're doing it for presentation purposes anyway. sprintf
can format any number almost any way you like, and lots more.
Full sprintf documentation: http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-sprintf
Solution 4
Edit
After getting feedback, It seems the original solution didn't work. That's why updated the answer as one of the suggestions.
def float_of_2_decimal(float_n)
float_n.to_d.round(2, :truncate).to_f
end
Other answers may work, if you want to have rounded numbers of 2 decimal places. But, If you want to have floating point numbers with first two decimal places without rounding, Those answers won't help.
So, to get a floating point number with first two decimal places, I used this technique. Doesn't work in some cases
def float_of_2_decimal(float_n)
float_n.round(3).to_s[0..3].to_f
end
with 5.666666666666666666666666
, it will return 5.66
instead of rounded 5.67
. Hope it will help someone
Related videos on Youtube
Sam
Updated on July 26, 2022Comments
-
Sam almost 2 years
How do you round a float to 2 decimal places in JRuby(1.6.x)?
number = 1.1164 number.round(2) # The above shows the following error # wrong number of arguments (1 for 0)
-
Michael Kohl almost 12 years
'%.2f' % number
also works is more commonly seen, at least in my experience. -
jaredsmith almost 10 yearsI know it does not appear to be the intention of Sam to round the number for the purpose of presenting something like a currency, but be aware that using #round(precision) will not work as intended if you are trying to do this (3.round(2) #=> 3.0, not 3.00). To get this, check out the answer by Theo below.
-
Lucy Bain over 9 years@MichaelKohl The ruby style guide favours
sprintf
(orformat
) over the%
version. Some reasoning for that is discussed here, it's mostly about readability. Not that we all have to follow the style guide, just giving some reasons :) -
The Gugaru over 8 yearsNo one should do this EVER when casting percentages. This is really bad form because by truncating you loose the ability to properly ROUND to the nearest 2 decimal positions. ie 0.455 if you just truncate you get 0.45 which is wrong for rounding because it should result in 0.46 . Never truncate a decimal always round the number else the result will be wrong when rounding up must ocurr.
-
ecoding5 about 8 yearsnote that after the 3rd decimal, sprintf rounds up on 6, not on 5, for instance, sprintf("%.3f", 1.2225) will be "1.222", sprintf("%.3f", 1.2226) will be "1.223", if that matters to you, stick to using #round
-
Anwar about 8 yearsBut
0.566666666666666
rounds to0.57
-
rorykoehler about 8 yearsThis doesn't work. In order to get it to work you need to account for any size number. Using the pattern implemented here you can:
def float_of_2_decimal(float_n) num = float_n.to_s.split('.') num[1] = num[1][0..1] num.join(".").to_f end
Or much simpler you can usefloat_n.to_d.round(2, :truncate).to_f
-
rorykoehler about 8 yearsAnything with a int bigger than 9 before the decimal place
-
Anwar about 8 yearsThank you for the point. But, your suggested methods fail on big numbers too!
-
Anwar about 8 yearsFor
11111111111111111.222222222222222
as input, first one shows1.11
and second one shows1.11111111111111e+16
-
rorykoehler about 8 yearsYes you are right ... once 16 places in front or over. Overflow issue. Best stick to big decimal if working with large numbers. Typecasting introduces problem
-
Anwar about 8 years@rorykoehler I've found another case, where my solution failed. That's why edited the answer.
-
rorykoehler about 8 years
float_n.to_d.round(2, :truncate).to_f
is a much better solution as it deals with very large numbers correctly. You should use that. -
Mirror318 almost 8 years
"%.2f"
rounds5
down, instead of up, is there any way to fix that? -
Bala Karthik almost 8 years
(5.6).round(2)
is returning only 5.6 -
Jared over 7 yearsThanks for answering. Could you please revise it to be in English, as the question was asked in English.
-
boulder_ruby over 7 yearsseems reasonable, that extra zero placeholder is still there, its just not visible
-
Dylan Vander Berg over 6 years@BalaKarthik That is the very reason why I am using the solution by Theo. It rounds correctly (except if you're going past 3 decimals for some odd reason, see comments), so that is the better solution if you are looking for string output.
-
gsumk over 4 yearsthis is right. I was generation random latitude and longitude so I use number.round(6) for 6 digits
-
lacostenycoder over 4 years@Anwar what is wrong with that? That's how rounding is supposed to work and does same for
.round(2)
. @Mirror318 are you sure about that? What is your example and on which version of ruby? -
lacostenycoder over 4 years@rorykoehler I tried your way and got:
'111111111111111111111111.222222'.to_d.round(2, :truncate).to_f
returns1.1111111111111111e+23
. But it works if you remove the last.to_f
-
Hamdan over 3 yearsthis should be marked as the correct answer.
-
ricks over 2 yearsThis produces a string, it should not be the correct answer for this question. You could run into issues if you have code expecting a decimal and you pass a string.
-
Theo over 2 years@ricks floating point numbers are binary, not decimal. There is no way to round to a specific number of decimals without changing the representation from binary. Using something like the DECIMAL type in many databases in one, but the most common reason to round is for presentation, so using a string is not unreasonable. If you round because of accounting rules you should start with a decimal representation and not a floating point.
-
ricks over 2 years@Theo Thanks i did not know that, for my use case i was writing a test for an accounting calculation with decimals and i wanted to compare the output (a price #.##). I guess i could have just used this and compared it to a string and it would have made no difference.