Round a Ruby Float Up or Down to the Nearest 0.05

Round a ruby float up or down to the nearest 0.05

Check this link out, I think it's what you need.
Ruby rounding

class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end

def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end

def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end
end

Round up instead of down when below zero

This should work.

> (-1.5+0.5).floor
=> -1
> (-1.4+0.5).floor
=> -1
> (-1.6+0.5).floor
=> -2

How to round decimal value up to nearest 0.05 value?

How about:

Math.Ceiling(myValue * 20) / 20

Rails Rounding float with different options

Here's a generic way to do it for any precision:

class Float
def round_currency(precision: 1, direction: :none)
round_method = case direction
when :none then :round
when :up then :ceil
when :down then :floor
end

integer_value = (self * 100).round
((integer_value / precision.to_f).send(round_method) * precision / 100.0)
end
end

# USAGE
9.37.round_currency(direction: :none, precision: 10)
# => 9.4

9.37.round_currency(direction: :up, precision: 25)
# => 9.5

9.37.round_currency(direction: :none)
# => 9.37

# Precision is defined in pennies: 10 dime, 25 quarter, 100 dollar. 1 penny is default

This code converts the float into an integer first to ensure accuracy. Be wary using ceil and floor with floating number arithmetic - due to accuracy errors you could get odd results e.g. 9.37 * 100 = 936.9999999999999. If you floor the result, you'll end up rounding to 9.36

Round up to the nearest tenth?

This works in general:

ceil(number*10)/10

So in Ruby it should be like:

(number*10).ceil/10.0

Why does this expression cause a floating point error?

It is not true that the significand of the floating-point format has enough bits to represent 26/65. (“Significand” is the preferred term. Significands are linear. Mantissas are logarithmic.)

The significand of a binary floating-point number is a binary integer. This integer is scaled according to the exponent. To represent 26/65, which is .4, in binary floating-point, we must represent it as an integer multiplied by a power of two. For example, an approximation to .4 is 1•2-1 = .5. A better approximation is 3•2-3=.375. Better still is 26•2-4 = .40625.

However, no matter what integer you use for the significand or what exponent you use, this format can never be exactly .4. Suppose you had .4 = f•2e, where f and e are integers. Then 2/5 = f•2e, so 2/(5f) = 2e, and then 1/(5f) = 2e-1 and 5f = 21-e. For that to be true, 5 would have to be a power of two. It is not, so you cannot have .4 = f•2e.

In IEEE-754 64-bit binary floating-point, the significand has 53 bits. With this, the closest representable value to .4 is 0.40000000000000002220446049250313080847263336181640625, which equals 3602879701896397•2-53.

Now let us look at your calculations. In a=0.05, 0.05 is converted to floating-point, which produces 0.05000000000000000277555756156289135105907917022705078125.

In a*26.0/65, a*26.0 is evaluated first. The exact mathematical result is rounded to the nearest representable value, producing 1.3000000000000000444089209850062616169452667236328125. Then this is divided by 65. Again, the answer is rounded, producing 0.0200000000000000004163336342344337026588618755340576171875. When Ruby prints this value, it apparently decides it is close enough to .02 that it can just display “.02” and not the complete value. This is reasonable in the sense that, if you convert the printed value .02 back to floating-point, you get the actual value again, 0.0200000000000000004163336342344337026588618755340576171875. So “.02” is in some sense a good representative for 0.0200000000000000004163336342344337026588618755340576171875.

In your alternative expression, you have a*=26.0/65. In this, 26.0/65 is evaluated first. This produces 0.40000000000000002220446049250313080847263336181640625. This is different from the first expression because you have performed the operations in a different order, so a different number was rounded. It may have happened that a value in the first expression was rounded down whereas this different value, because of where it happened to land relative to values representable in floating-point, rounded up.

Then the value is multiplied by a. This produces 0.02000000000000000388578058618804789148271083831787109375. Note that this value is further from .02 than the result of the first expression. Your implementation of Ruby knows this, so it determines that printing “.02” is not enough to represent it accurately. Instead, it displays more digits, showing 0.020000000000000004.

Removing scientific notation from float

You can use string formatting.

a =  0.0004 * 0.0000000000012 # => 4.8e-16
'%.5f' % a # => "0.00000"

pi = Math::PI # => 3.141592653589793
'%.5f' % pi # => "3.14159"

MYR Currency Rounding

You could take an offset and take a multiple floored value.

If you need zeroes at the end take .toFixed(2).

const format = f => Math.floor((f + 0.025) * 20) / 20;

console.log([1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19].map(format));
.as-console-wrapper { max-height: 100% !important; top: 0; }


Related Topics



Leave a reply



Submit