473,412 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Formatting the decimal value

440 256MB
Hi,

I have the following row of data.

I/P:
20200107 1180.3489999999999 223.56720000000001 191.21019999999999

I would like to write this data to the file with two decimal places.

O/P:
20200107 1180.35 223.57 191.21

What is the syntax of it?.

Thanks
PSB
Mar 19 '07 #1
13 1892
bartonc
6,596 Expert 4TB
Hi,

I have the following row of data.

I/P:
20200107 1180.3489999999999 223.56720000000001 191.21019999999999

I would like to write this data to the file with two decimal places.

O/P:
20200107 1180.35 223.57 191.21

What is the syntax of it?.

Thanks
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> argStr = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
  2. >>> argList = argStr.split()
  3. >>> a = int(argList[0])
  4. >>> b, c, d = (float(x) for x in argList[1:])
  5. >>> "%d %.2f %.2f %.2f" %(a, b, c, d)
  6. '20200107 1180.35 223.57 191.21'
  7. >>> 
Mar 20 '07 #2
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. a = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
  2. print a.split()[0] ,
  3. for i in [ "%.2f" % float(i) for i in a.split()[1:] ]: print i,
  4.  
Mar 20 '07 #3
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. a = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
  2. print a.split()[0] ,
  3. for i in [ "%.2f" % float(i) for i in a.split()[1:] ]: print i,
  4.  
So, is this a list comprehension to avoid using a generator due to the newness of generators?
Mar 20 '07 #4
ghostdog74
511 Expert 256MB
So, is this a list comprehension to avoid using a generator due to the newness of generators?
No. Its just a way to handle OP's problem. you can use generators too.
eg
Expand|Select|Wrap|Line Numbers
  1. ...
  2. for i in ("%.2f" % float(i) for i in a.split()[1:] ):
  3.      print i
  4. ....
  5.  
generators can conserve memory(supposedly from docs) better than list comprehension, especially with large lists.
Mar 20 '07 #5
psbasha
440 256MB
Thnaks for the reply,

The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file

I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}

Regard
PSB
Mar 21 '07 #6
bartonc
6,596 Expert 4TB
Thnaks for the reply,

The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file

I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}

Regard
PSB
Oh... Now you tell us:
Expand|Select|Wrap|Line Numbers
  1. >>> help(dict.get)
  2. Help on method_descriptor:
  3.  
  4. get(...)
  5.     D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
  6.  
  7. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  8. >>> key = 20200107
  9. >>> l = d.get(key, (0, 0, 0))
  10. >>> s = "%d %.2f %.2f %.2f" %(key, l[0], l[1], l[2])
  11. >>> s
  12. '20200107 1180.35 223.57 191.21'
  13. >>> 
Mar 21 '07 #7
bvdet
2,851 Expert Mod 2GB
Thnaks for the reply,

The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file

I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}

Regard
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  2. >>> print ' '.join([str(d.items()[0][0]), ] + [s for s in ['%0.2f' % float(i) for i in d.items()[0][1]]])
  3. 20200107 1180.35 223.57 191.21
  4. >>> 
If you know the key as in:
Expand|Select|Wrap|Line Numbers
  1. for key in d:
  2.     print ' '.join([str(key), ] + [s for s in ['%0.2f' % float(i) for i in d[key]]])
  3.  
If you are writing multiple items from your dictionary to your file, be sure to add a newline character '\n' at the end of each line.
Mar 21 '07 #8
bartonc
6,596 Expert 4TB
Oh... Now you tell us:
Expand|Select|Wrap|Line Numbers
  1. >>> help(dict.get)
  2. Help on method_descriptor:
  3.  
  4. get(...)
  5.     D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
  6.  
  7. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  8. >>> key = 20200107
  9. >>> l = d.get(key, (0, 0, 0))
  10. >>> s = "%d %.2f %.2f %.2f" %(key, l[0], l[1], l[2])
  11. >>> s
  12. '20200107 1180.35 223.57 191.21'
  13. >>> 
and for a real world application with more that one entry in the dictionary:
Expand|Select|Wrap|Line Numbers
  1. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999],
  2. ... 20200108:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999],
  3. ... 20200109:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  4. ... 
  5. >>> for k, v in d.items():
  6. ...     print "%d %.2f %.2f %.2f" %(k, v[0], v[1], v[2])
  7. ...     
  8. 20200107 1180.35 223.57 191.21
  9. 20200108 1180.35 223.57 191.21
  10. 20200109 1180.35 223.57 191.21
  11. >>> 
Mar 21 '07 #9
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  2. >>> print ' '.join([str(d.items()[0][0]), ] + [s for s in ['%0.2f' % float(i) for i in d.items()[0][1]]])
  3. 20200107 1180.35 223.57 191.21
  4. >>> 
If you know the key as in:
Expand|Select|Wrap|Line Numbers
  1. for key in d:
  2.     print ' '.join([str(key), ] + [s for s in ['%0.2f' % float(i) for i in d[key]]])
  3.  
If you are writing multiple items from your dictionary to your file, be sure to add a newline character '\n' at the end of each line.
Dang, that's good! And fast; you beat me by one minute!
Mar 21 '07 #10
bvdet
2,851 Expert Mod 2GB
Dang, that's good! And fast; you beat me by one minute!
Fastest is not necessarily the bestest! There was something in there that was redundant. Maybe this is it:
Expand|Select|Wrap|Line Numbers
  1. d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  2.  
  3. print ' '.join([str(d.items()[0][0]), ] + ['%0.2f' % float(i) for i in d.items()[0][1]])
  4.  
  5. # If you know the key, as in:
  6. for key in d:
  7.     print ' '.join([str(key), ] + ['%0.2f' % float(i) for i in d[key]])
>>> 20200107 1180.35 223.57 191.21
20200107 1180.35 223.57 191.21
>>>
Mar 21 '07 #11
ghostdog74
511 Expert 256MB
just another variation
Expand|Select|Wrap|Line Numbers
  1. >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
  2. >>> for i,j in d.iteritems():
  3. ...  print "%s %s" % ( i , ' '.join(["%.2f" % float(f) for f in j]) )
  4. ...
  5. 20200107 1180.35 223.57 191.21
  6.  
  7.  
Mar 21 '07 #12
psbasha
440 256MB
Thanks to all.

It was really helpful for validating my geometry data.

-PSB
Mar 21 '07 #13
bartonc
6,596 Expert 4TB
Thanks to all.

It was really helpful for validating my geometry data.

-PSB
Which version did you end up going with?
Mar 21 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Charles Kerekes | last post by:
Hello everyone, I am still learning C++, so I'm not sure what I'm trying to do is possible. Her is a simplified example: char Test = 'L'; cout << "Test Char as decimal: " << dec << Test <<...
36
by: Andrew | last post by:
I trying to format my output to display a set number of decimal places. I have been trying to use the <iomanip> setprecision(), but that will only display the total number of digits. Can someone...
2
by: Guoqi Zheng | last post by:
Dear Sir, I am trying to get one decimal value from DB. However, every time, I got an INT back instead of decimal. For example, if I have 4.75 in DB, I will get a 5 back. The followings are...
1
by: ven | last post by:
hello i`m makin a asp.net service with database connection where i have an insert with decimal value, when i run my function i get this error : There are fewer columns in the INSERT statement...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
1
by: Microsoft News Group | last post by:
I am trying to make a column which is decimal format, convert nulls to a certain string such as "No Max". This is for a price range, and if they do not put a value in, a null value is put in...
7
by: Sukanta Bhattacharjee | last post by:
Hi, I am in big trouble for the last 10 days and not got any solution in the web. My problem is : I have a project in .NET 1.1 version, which I have transfered to .NET 2.0 version , almost all are...
11
by: irkahs | last post by:
Hello all, I ask this question because I searched and did not find anything that related to this query. My apologies if this is a mistake. Please see the following code. I am using...
4
OuTCasT
by: OuTCasT | last post by:
I have been trying to save a decimal value into my sql database, what do you use , money, decimal, smallmoney, int ???
1
by: pankajprakash | last post by:
Hi, I have a decimal number. I need to convert this decimal value to exponential value. I have a vb.net code to convert from decimal to exponential value is Format(100000, #.0#E-##) . When I run this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.