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. 8 1800
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
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
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
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
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"
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
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?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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
--...
|
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...
|
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...
|
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...
|
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);
|
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 =...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |