Truncate number which ends with 0 decimal

Hi All.

The question is how I can do to truncate a float number ending in 0.

I have an exercise where I truncate a float number to 2 decimals, the
problem is that if the decimal ends in 0 instead of return the full
number, only returns the first number.

For example:

53454545.5459332345 >>> 53454545.54 is OK.

Now, this number:

948484747.5000000 >>> 948484747.5 is incorrect because it should be
948484747.50

To truncate the number to 2 decimals I’m using this code:

###################################

(number*100).to_i / 100.0

##################################

which works correctly if the truncated decimal number does not end in 0.

Other examples:

#####################################################################

857474.555586767 ===> 857474.55 ==> is ok.
857474.50000000 ====> 857474.5 ==> is wrong >> Should be 857474.50

8120384.9876355 ===> 8120384.98 ==> is ok.
8120384.9000000 ===> 8120384.9 ==> is wrong >> Should be 8120384.90

#####################################################################

The question is whether there is some other way, that if the decimal
number ends in 0, not truncate the first issue, and I return the full
number.

Thanks.

On 2012-10-18 22:34, Joao S. wrote:

Hi All.

The question is how I can do to truncate a float number ending in 0.

$ irb

class Numeric; def to_2; “%.2f” % self end end
=> nil

2345.5000.to_2
=> “2345.50”

2.to_2
=> “2.00”

Hi,

this is a big misunderstanding of numbers. You mistake the
representation of a number for the number itself. 948484747.5000000,
948484747.50 and 948484747.5 are all the exact same number. So trying to
“convert” one to another makes absolutely no sense.

The only difference is how the number is written down. So what you’re
actually looking for is not a number operation but a specific way of
converting a number to a string. You want the decimal representation of
the number with exactly two decimal places. And Wybo showed you the
typical way of doing this.

Wybo D. wrote in post #1080338:

On 2012-10-18 22:34, Joao S. wrote:

Hi All.

The question is how I can do to truncate a float number ending in 0.

$ irb

class Numeric; def to_2; “%.2f” % self end end
=> nil

2345.5000.to_2
=> “2345.50”

2.to_2
=> “2.00”

This code works fine when the decimal ends in zero, but if the decimal
number ends in another, round it, and that’s not what I want.

class Numeric
def to_2
“%.2f” % self
end
end

2345.5000.to_2 ==>> 2345.50 is ok.
2345.5876374536 ==>> 2345.59 ==>> is wrong ==>> should be 2345.58

1234.567889 ==>> 1234.57 ==>> is wrong ==>> should be 1234.56
2659876.76789999 ==>> 2659876.77 ==> is wrong ==>> should be 2659876.76

On 19/10/2012, at 12:05 PM, Joao S. wrote:

1234.567889 ==>> 1234.57 ==>> is wrong ==>> should be 1234.56
2659876.76789999 ==>> 2659876.77 ==> is wrong ==>> should be 2659876.76

2659876.77 is correct. This is how numbers ‘work’. There is either a
fundamental mis-understanding of numbers or you really want strings.

Henry

Henry M. wrote in post #1080347:

2659876.77 is correct. This is how numbers ‘work’. There is either a
fundamental mis-understanding of numbers or you really want strings.

Have you ever heard of truncating?

When a number is “rounded” differently from what we know from school
(which is commercial rounding) doesn’t mean it is “wrong”. You can round
any way you like.

Joao S. wrote in post #1080345:

This code works fine when the decimal ends in zero, but if the decimal
number ends in another, round it, and that’s not what I want.

Just make the precision 3 and cut off the last character:

p format(’%.3f’, 2345.5876374536).chop
p format(’%.3f’, 2345.5).chop

On Thu, Oct 18, 2012 at 11:55 PM, Jan E. [email protected] wrote:

When a number is “rounded” differently from what we know from school
(which is commercial rounding) doesn’t mean it is “wrong”. You can round
any way you like.

Amen. See: BigDecimal::ROUND_MODE

(http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-mode)

H*

On Fri, Oct 19, 2012 at 1:04 AM, Jan E. [email protected]ain.invalid wrote:

this is a big misunderstanding of numbers. You mistake the
representation of a number for the number itself. 948484747.5000000,
948484747.50 and 948484747.5 are all the exact same number. So trying to
“convert” one to another makes absolutely no sense.

I couldn’t agree more. Usually formatting should only be applied on
output (i.e. presentation to the user, printf comes to mind). Before
that rounding should be avoided to keep calculations as precise as
possible.

Kind regards

robert