472,958 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Floating Number format problem


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.

Jun 12 '07 #1
8 1785
ici
On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
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.
>>print "%.02f" % (2324.012)
2324.01
>>>
http://docs.python.org/tut/node9.htm...00000000000000

Jun 12 '07 #2
On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
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.
>print "%.02f" % (2324.012)
2324.01

http://docs.python.org/tut/node9.htm...00000000000000
thank you !
But in these case:
>>print '%.02f'%1.002
1.00
>>print '%.02f'%1.00
1.00

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

Jun 12 '07 #3
En Tue, 12 Jun 2007 05:46:25 -0300, <ke********@gmail.comescribió:
On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
>On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
print "%.02f" % (2324.012)
2324.01
But in these case:
>>>print '%.02f'%1.002
1.00
>>>print '%.02f'%1.00
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))

--
Gabriel Genellina

Jun 12 '07 #4
En Tue, 12 Jun 2007 05:46:25 -0300, <ke********@gmail.comescribió:
On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
>On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
print "%.02f" % (2324.012)
2324.01
But in these case:
>>>print '%.02f'%1.002
1.00
>>>print '%.02f'%1.00
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))

--
Gabriel Genellina

Jun 12 '07 #5
Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Tue, 12 Jun 2007 05:46:25 -0300, <ke********@gmail.comescribió:
>On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
>>On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:

How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
print "%.02f" % (2324.012)
2324.01
But in these case:
>>>>print '%.02f'%1.002
1.00
>>>>print '%.02f'%1.00
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"
Jun 12 '07 #6
Marc Christiansen wrote:
Gabriel Genellina <ga*******@yahoo.com.arwrote:
>En Tue, 12 Jun 2007 05:46:25 -0300, <ke********@gmail.comescribió:
>>On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:

How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
print "%.02f" % (2324.012)
2324.01
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:
>>("%.2f" % 100.0).rstrip(".0")
'1' # wrong
>>("%.2f" % 100.0).rstrip("0").rstrip(".")
'100'

Peter
Jun 12 '07 #7
On Jun 12, 8:04 pm, Marc Christiansen <use...@solar-empire.dewrote:
Gabriel Genellina <gagsl-...@yahoo.com.arwrote:
En Tue, 12 Jun 2007 05:46:25 -0300, <kelvin....@gmail.comescribió:
On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
print "%.02f" % (2324.012)
2324.01
But in these case:
>>>print '%.02f'%1.002
1.00
print '%.02f'%1.00
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"
Marc, thanks for coming, but:
>>my_other_formatter_ommitting_trailing_zeroes(100 .00)
'1'
>>>
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?

Jun 12 '07 #8
Peter Otten <__*******@web.dewrote:
Marc Christiansen wrote:
>Gabriel Genellina <ga*******@yahoo.com.arwrote:
>>En Tue, 12 Jun 2007 05:46:25 -0300, <ke********@gmail.comescribió:

On 6 12 , 3 16 , ici <iltch...@gmail.comwrote:
On Jun 12, 10:10 am, <kelvin....@gmail.comwrote:
>
How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
>print "%.02f" % (2324.012)
2324.01
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:
>>>("%.2f" % 100.0).rstrip(".0")
'1' # wrong
>>>("%.2f" % 100.0).rstrip("0").rstrip(".")
'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
Jun 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
12
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
25
by: Gaurav Verma | last post by:
Hi, I want to convert a floating point number (or a decimal number say 123.456) into binary notation using a C program. Can somebody help me out with it? Thanks Gaurav --...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
2
by: ngn | last post by:
How do you format the number of decimal places in a floating point number converted to a string? I simply use the ToString() method on the float variable, but I cannot find how to format it a...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
14
by: mathieu | last post by:
hi there, I do not understand the syntax for ios_base::precision. Let say I have a floating point: const float f = 0.313244462; How do I write is to a stream ? const float f =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.