How to truncate/round-off decimal numbers?

G

Girish Sahani

Hi,

I want to truncate every number to 2 digits after the decimal point. I
tried the following but it doesnt work.
0.67000000000000004

Inspite of specifying 2 in 2nd attribute of round, it outputs all the
digits after decimal.
 
S

Sybren Stuvel

Girish Sahani enlightened us with:
I want to truncate every number to 2 digits after the decimal point. I
tried the following but it doesnt work.

Yes it does, read the documentation about floating points.
0.67000000000000004

If you want to format it, use '%.2f' % (float(a)/b)

Sybren
 
M

MTD

a = 2
0.67000000000000004

Inspite of specifying 2 in 2nd attribute of round, it outputs all the
digits after decimal.

This is because of floating point inaccuracy. The system cannot
accurately represent some integers, however it does its best to
approximate them. Notice this:
'0.67'
 
L

Laurent Pointal

Girish Sahani a écrit :
Hi,

I want to truncate every number to 2 digits after the decimal point. I
tried the following but it doesnt work.

0.67000000000000004

Inspite of specifying 2 in 2nd attribute of round, it outputs all the
digits after decimal.

There are two operations:

1) calculate of round(), which return a float number result, with the
well known problem of floating point numbers represantation (see the FAQ).

2) print that number, where default printing of last expression result
in the cli interpreter displays up to the highest precision, print
statement works differently:

A+

Laurent.
 
S

Scott David Daniels

Sybren said:
Girish Sahani enlightened us with:

If you want to format it, use '%.2f' % (float(a)/b)

Sybren has this right. If you follow everyone else's advice,
you'll eventually discover:
only gives you "2.2", not "2.20" (which is what, I suspect, you want).

--Scott David Daniels
(e-mail address removed)
 
A

Aahz

I want to truncate every number to 2 digits after the decimal point. I
tried the following but it doesnt work.

0.67000000000000004

Inspite of specifying 2 in 2nd attribute of round, it outputs all the
digits after decimal.

You should also consider switching to the decimal module if you care
about getting correct results.
 
P

per9000

Hi,

just a thought: if you *always* work with "floats" with two decimals,
you are in fact working with integers, but you represent them as a
floats - confusing for the internal representation.

So why not work with int(float * 100) instead? This way you only have
to take care of roundoffs etc when dividing.

"int (+|-|*) int" = int
"int / int" = int / int + int % int

Integers are nice, me like integers.

/per9000
 
J

jean-michel bain-cornu

just a thought: if you *always* work with "floats" with two decimals,
you are in fact working with integers, but you represent them as a
floats - confusing for the internal representation.

So why not work with int(float * 100) instead? This way you only have
to take care of roundoffs etc when dividing.
And why won't you work with decimal module ?
 
N

Nick Maclaren

|>
|> just a thought: if you *always* work with "floats" with two decimals,
|> you are in fact working with integers, but you represent them as a
|> floats - confusing for the internal representation.

No, you aren't - you are working with fixed-point, which is something
that is neither integers nor floating-point, but is somewhere in
between. I am (just) old enough to remember when it was used for
numeric work, and to have used it for that myself, but not old enough
to have done any numeric work using fixed-point hardware.

|> So why not work with int(float * 100) instead? This way you only have
|> to take care of roundoffs etc when dividing.

And multiplying, and calling most mathematical functions.


Regards,
Nick Maclaren.
 
P

per9000

Nick said:
|> just a thought: if you *always* work with "floats" with two decimals,
|> you are in fact working with integers, but you represent them as a
|> floats - confusing for the internal representation.

No, you aren't - you are working with fixed-point

Nick, your answer has so many layers, I'll try to explain how I think
:-D

1) if you use integers you can think of them as having one part bigger
than 100 and one part smaller than 100, like so:(111, 22)
Here the output 111,22 looks like something else than an integer, but
this is just a matter of representation. a *is* an integer, but we
represent it *as if it was* a "decimal" number. (Compare with
(minutes,seconds) or (euro,cents) or (feet,inch) or any other
"arbitrary" position system)

2) If we use floats with two decimals222.33000000000001
they look like fix-point numbers (I had to look it up
http://en.wikipedia.org/wiki/Fixed-point :-D) but python stores it
(correct me if I am wrong) as a float (or double or quad or whatever).
If we want to work with fix-point aritmetics we have to invent new
functions to do most math.

3) Most "decimal numbers" cannot be stored exactly as floats - that is
why b gave the ugly print. But some can, f.x0.25
quart translates to a finite "decimal" number in binary (0.01 I think).

The point is: representing should-be integers as floats makes you loose
precision (negligable probalby but still...).

4)
|> So why not work with int(float * 100) instead? This way you only have
|> to take care of roundoffs etc when dividing.

And multiplying, and calling most mathematical functions.
You are correct of course. My mistake.

But, the multiplication is exact before you start rounding off - I wont
start counting ordos for int*int vs. float*float, but it could have
some advantages(24727, 5426)
On the other hand you will quickly loose accuracy if you perform
multiple multiplications or divisions or use other mathematical
functions.

5) So, when could this way of thinking be useful? Well, rarely, but if
you only add/subtract "decimals" and/or multiply "decimals" with whole
numbers or if you want to use some non-metric system to do scientific
stuff (compute square feet when having (feet1,inch1) * (feet2,inch2)
assuming that inches are atomic.)

This could of course be extended to
(feet, inch, quarter_of_afoot, sixteeth_of_a_foot) if you'd like - it
is all a matter of representation.

Regards,
Per
----
"It is a gift. A gift to the foes of 'the Terrorists'. Why not
use this 'terrorism'? Long has my father, 'George Bush Sr',
kept the forces of 'the terrorists' at bay. By the blood of
our people are your lands kept safe. Give 'the land of the
brave' the weapon of the enemy. Let us use it against him."
 
S

Scott David Daniels

Nick Maclaren wrote: (of fixed point)
.... I am (just) old enough to remember when it was used for
numeric work, and to have used it for that myself, but not old enough
to have done any numeric work using fixed-point hardware.

You are using fixed point hardware today. Fixed point tracked the
"decimal point" (or "binary point" or whatever) in the mind of the
programmer, not in the state of the hardware. There never was
"fixed point hardware," it was simply a way of viewing the results
of the gates of the same hardware we use today.
 
N

Nick Maclaren

|> Nick Maclaren wrote: (of fixed point)
|> > .... I am (just) old enough to remember when it was used for
|> > numeric work, and to have used it for that myself, but not old enough
|> > to have done any numeric work using fixed-point hardware.
|>
|> You are using fixed point hardware today. Fixed point tracked the
|> "decimal point" (or "binary point" or whatever) in the mind of the
|> programmer, not in the state of the hardware. There never was
|> "fixed point hardware," it was simply a way of viewing the results
|> of the gates of the same hardware we use today.

Er, no. Analogue hardware never was fixed-point, and I have used that :)

More relevantly, you are using a very parochial and unusual interpretation
of the word 'hardware'. In normal usage, it includes everything below
the instruct set specification (now often called the ABI). And I can
assure you that there is a considerable difference between the hardware
that provides a floating-point interface at the ABI and that which
provides a fixed-point interface.


Regards,
Nick Maclaren.
 
Joined
Dec 17, 2011
Messages
1
Reaction score
0
similar problem

floatVal = .4350001
"%.2f" % floatVal

To get .43 works great, but what if I wanted to have the decimal of what I truncate to be of variable input?
it's not like it would work this way:

accuracy = 2
floatVal = .4350001

"%.%if" %(floatVal, min_accuracy)

I'm trying to get the pointer "accuracy" as a value that can be changed/entered via user input so users can change what decimal place to round to. How would I do that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top