pgega
(pgega)
September 16, 2007, 2:50pm
1
Hi there
I am going to calculate VAT at one of my websites, and UK VAT is 17.5
%.
So the question is how to round up some value to two decimal points?
I often get values like 6,991..
In php I was using round function
round($price, 2)
is in ruby something similat to PHP's round function where I could
specify number of decimal places ?
P.
Summercool
(Summercool)
September 16, 2007, 3:18pm
2
one way is to use round(n * 100) / 100.0
Summercool
(Summercool)
September 16, 2007, 3:20pm
3
in Ruby, it is like (n * 100).round / 100.0
pgega
(pgega)
September 16, 2007, 3:54pm
4
Thanks,
And which part of the code corresponds for the number of decimal
places I want to round?
(I am sorry sorry for killing you, I am just a small designer.)
Summercool
(Summercool)
September 16, 2007, 4:01pm
5
it is like (n * 10**2).round / (10**2).to_f
the 2 is the one you want and 10**2 is 10 to the power of 2.
n would be your number, then multiplying it by 100 and rounding it
would round it to 2 decimal places, then divide it by 100 to get your
decimal backā¦
n = 12.3456
n * 100 = 1234.56
round(n) = 1235
1235 / 100 = 12.35
pgega wrote:
pgega
(pgega)
September 16, 2007, 4:03pm
7
and as I assume n is my value I want to round ?
sorry, line 3 should be n.round()
William Pratt wrote:
pgega
(pgega)
September 16, 2007, 4:09pm
9
n = 12.3456
n * 100 = 1234.56
round(n) = 1235
1235 / 100 = 12.35
So clear , so easy.
Thanks a lot.