Floating Number format problem

  • Thread starter =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z
  • Start date
?

=?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z

How could I format the float number like this: (keep 2 digit
precision)
1.002 => 1
1.12 => 1.12
1.00 => 1
1.567 => 1.57
2324.012 => 2324.01

I can not find any Formatting Operations is able to meet my
requirement.
Any suggestion will be appreciated.
 
G

Gabriel Genellina

But in these case:

1.00

I just expect it to output "1" , but these way will output 1.00

def my_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
if result[-3:]=='.00': result = result[:-3]
return result

for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
print "%g -> %s" % (f, my_formatter_ommitting_trailing_zeroes(f))
 
G

Gabriel Genellina

But in these case:

1.00

I just expect it to output "1" , but these way will output 1.00

def my_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
if result[-3:]=='.00': result = result[:-3]
return result

for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
print "%g -> %s" % (f, my_formatter_ommitting_trailing_zeroes(f))
 
M

Marc Christiansen

Gabriel Genellina said:
But in these case:

1.00

I just expect it to output "1" , but these way will output 1.00

def my_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
if result[-3:]=='.00': result = result[:-3]
return result

for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
print "%g -> %s" % (f, my_formatter_ommitting_trailing_zeroes(f))

Or:

def my_other_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
return result.rstrip('0.')

my_other_formatter_ommitting_trailing_zeroes(1.102) == "1.1"
 
P

Peter Otten

Marc said:
Or:

def my_other_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
return result.rstrip('0.')

Make that result.rstrip("0").rstrip("."), or it may fail:
'100'

Peter
 
J

John Machin

Gabriel Genellina said:
En Tue, 12 Jun 2007 05:46:25 -0300, <[email protected]> escribió:
def my_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
if result[-3:]=='.00': result = result[:-3]
return result
for f in [1.0, 1.002, 1.12, 1.567, 2324.012]:
print "%g -> %s" % (f, my_formatter_ommitting_trailing_zeroes(f))

Or:

def my_other_formatter_ommitting_trailing_zeroes(value):
result = '%.2f' % value
return result.rstrip('0.')

my_other_formatter_ommitting_trailing_zeroes(1.102) == "1.1"

Marc, thanks for coming, but:

What does the OP want to happen with 1.2? I suspect he wants '1.2',
not '1.20'

Looks like a variation of Marc's idea will do the business:
values = [100.0, 1.0, 1.2, 1.002, 1.12, 1.567, 2324.012]
[('%.02f' % x).rstrip('0').rstrip('.') for x in values]
['100', '1', '1.2', '1', '1.12', '1.57', '2324.01']

Howzat?
 
M

Marc Christiansen

Peter Otten said:
Make that result.rstrip("0").rstrip("."), or it may fail:

'100'

Oops, didn't think of what happens if the value is a multiple of 10.
Thanks for correcting me. And thanks to John, who found it too.

Marc
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top