473,915 Members | 5,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float to string with different precision

Hi,

I have to print float numbers to a file. Each float should be 5
characters in width (4 numbers and the decimal point).
My problem is that I do not now how to specify float to have different
numbers of decimals. For example

5.32 -5.320
10.356634 -10.357
289.234 -289.2

In the string formating operations only fixed number of decimal digits
is allow.

Thanks in advance for the help,

Zunbeltz

Aug 10 '07 #1
3 4841
zu******@gmail. com wrote:
I have to print float numbers to a file. Each float should be 5
characters in width (4 numbers and the decimal point).
My problem is that I do not now how to specify float to have different
numbers of decimals. For example

5.32 -5.320
10.356634 -10.357
289.234 -289.2

In the string formating operations only fixed number of decimal digits
is allow.
>>["%#.4g" % f for f in [5.32, 10.356634, 289.234, 123456789.]]
['5.320', '10.36', '289.2', '1.235e+08']

Found by playing around with format strings, so no guarantees.

Peter
Aug 10 '07 #2
On Aug 10, 1:12 am, Peter Otten <__pete...@web. dewrote:
zunbe...@gmail. com wrote:
I have to print float numbers to a file. Each float should be 5
characters in width (4 numbers and the decimal point).
My problem is that I do not now how to specify float to have different
numbers of decimals. For example
5.32 -5.320
10.356634 -10.357
289.234 -289.2
In the string formating operations only fixed number of decimal digits
is allow.
>["%#.4g" % f for f in [5.32, 10.356634, 289.234, 123456789.]]

['5.320', '10.36', '289.2', '1.235e+08']

Found by playing around with format strings, so no guarantees.

Peter
If the above does not work
[/code]test_list = [ 5.32, 10.35634, 289.234 ]
for num in test_list :
str_num = "%11.5f" % (num) ## expand to at least 5
print str_num, "-->", str_num.strip()[:5][/code]

Aug 10 '07 #3
On Aug 10, 8:37 am, Zentrader <zentrad...@gma il.comwrote:
>
If the above does not work
[/code]test_list = [ 5.32, 10.35634, 289.234 ]
for num in test_list :
str_num = "%11.5f" % (num) ## expand to at least 5
print str_num, "-->", str_num.strip()[:5][/code]
This has the disadvantage that it doesn't round the last digit. For
example 10.356634 yields 10.356 instead of 10.357.

You can use '*' in format strings to take a numeric field value from a
variable. For example

ndecimals = 2
print "%5.*f" % (ndecimals, x)

formats x with 2 digits following the decimal point. Or you can
simply
cobble up a format string at run time:

format = "%%5.%df" % ndecimals
print format % x

Aug 10 '07 #4

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

Similar topics

2
2018
by: j vickroy | last post by:
Howdy, I do not understand the following behavior for: PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32. >>> >>> float('0.0') 0.0 >>> row = ('0.0', '1.0', None)
3
108658
by: Erik2000 | last post by:
If someone defines a float and double in C++, does that say anything about how many digits the number will hold at maximum and how many places there will be after the decimal point? Or is this open to interpretation? -- Posted via http://dbforums.com
13
5360
by: Sebastian | last post by:
Hi, yesterday I discovered something that won't come as a surprise to more experienced C++ - programmers - but I was genuinely amazed. I had this line of code: float x = 0.9f; and when I had a look at it in the VS.NET 2003 debugger, it said x had avalue of 0.89999998 ! When I printf'ed x to a logfile, it was 0.899999976158142090000000000000. Now, why on earth is that? I understand that the results of floatingpoint operations will always...
1
2689
by: Nishant Deshpande | last post by:
On Solaris 64-bit: sizeof(long) = 8 sizeof(double) = 8 !!! So i figure i can't store a long with the same precision in a double. But, when i create a long and assign its largest value to it, then assign this to a double, then subtract the double from the long, I get 0.
3
23771
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled by C# or they are hardware (processor) dependent. Basic addition operation errors, for ex: 3.6 - 2.4 = 1.19999999999 or 1.20000000003 There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please reply me how these operations are...
2
2563
by: mac | last post by:
Assume you have a textbox named "displayValue" What is the difference (if any) between these two approaches: 1.) float var; var = 0.42F; displayValue.Text = var.ToString 2.)
2
7182
by: ibcarolek | last post by:
We have a field which is decimal (9,2) and another which is decimal (9,3). Is there anyway to subtract the two and get a precision 3 value without changing the first field to 9,3? For instance, retail value is 9,2, but our costs are at 9,3 due to being averaged. To calculate margin (retail-cost), we want that also to be 9,3, but a basic subtraction comes out 9,2. You can see we don't want to increase retail to be 9,3 (that would look...
6
2628
by: John [H2O] | last post by:
I have a script: from numpy import float OutD= v= OutD.append(]) On linux: Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
0
1045
by: Christian Heimes | last post by:
John wrote: I've fixed the issue for Python 2.6 and 3.0 a while ago. Mark and I have spent a lot of time on fixing several edge cases regarding inf, nan and numerical unsound functions in Python's math und cmath module. Christian
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4778
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.