0
x="1,234.00"
y =x.replace(',','')
k = float(y)
print k

output=1234.0 but i need 1234.00 value please solve this problem

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Lakshmi
  • 1
  • 2
  • print ('%.2f' % 1234.0) print type('%.2f' % 1234.0) output: 1234.00 but i need that type also float please solve this problem – Lakshmi Mar 25 '16 at 08:38
  • The problem is not a duplicate, but that has not at all become clear from the question itself. – Peter Mar 25 '16 at 09:32

1 Answers1

0

There is no difference in value between 1234.0 and 1234.00. You can't save more or less non-significant digits in a float. However, you can print more or less (non-)significant digits. In older versions of python you can use the % method (documentation). In Python 2.7 and up, use the string format method. An example:

f = float('156.2')

# in Python 2.3
no_decimals = "%.0f" % f
print no_decimals  # "156" (no decimal numbers)

five_decimals = "%.5f" % f
print five_decimals  # "156.20000" (5 decimal numbers)

# in Python 2.7
no_decimals = "{:.0f}".format(f)
print no_decimals  # "156" (no decimal numbers)

five_decimals = "{:.5f}".format(f)
print five_decimals  # "156.20000" (5 decimal numbers)

If you for some reason have no access to the code that prints the value, you can create your own class, inherit from float and supply your own __str__ value. This could break some behaviour (it shouldn't, but it could), so be careful. Here is an example:

class my_float(float):
    def __str__(self):
        return "%.2f" % self

f = my_float(1234.0)
print f  # "1234.00"

(I ran this on Python 2.7, I have 2.3 not installed; please consider upgrading Python to 2.7 or 3.5; 2.3 is seriously outdated.)

Peter
  • 2,932
  • 1
  • 12
  • 9
  • Yes python 2.7 is upgraded,but we are using only python 2.3.need it only 2.3 – Lakshmi Mar 25 '16 at 08:09
  • There is no difference between 1234.0 and 1234.00.but when i comparison to webpage product price value like 1234.00 and this value 1234.0.getting error – Lakshmi Mar 25 '16 at 08:16
  • @nagalakshmimallempati So you're comparing strings rather than floats? The `format` method should help you with that. I'll adapt my answer so it is more clear you can create a string and not only print it. – Peter Mar 25 '16 at 08:22
  • yes i comparing floats.but using only python 2.3 no need upgraded version. python 2.3 should not have format method. – Lakshmi Mar 25 '16 at 08:26
  • Ah, right. I adapted my answer to include the `%` method. – Peter Mar 25 '16 at 08:40
  • print ('%.2f' % 1234.0) print type('%.2f' % 1234.0) output;1234.00 but i need float data type.please help me – Lakshmi Mar 25 '16 at 08:44
  • Like I already wrote, what you want does not exist. There is no difference between `float('1234.0')` and `float('1234.00')`. You say you get 'an error' when comparing to a 'website value'. What error is that? What is the 'website value'? A string? A float? – Peter Mar 25 '16 at 08:48
  • website value is float' 1,699.00' and replace the comma and convert this string value to float then i got 1699.0.i know no difference between 1699.00 and 1699.0.i want set this value to another field like metaprice. But meta price is float and my value is also float.but in my value missed one zero.so it was not working.if i use above % method again it is become a string. – Lakshmi Mar 25 '16 at 08:58
  • What do you mean by 'set to another field'? If meta-price is a float, and you replace it by another float, there is no problem, right? Only when you print out the value somewhere, it matters how many decimal numbers you see. – Peter Mar 25 '16 at 09:06
  • print type(f) five_decimals = "%.5f" % f print five_decimals print type(five_decimals) output: 156.20000 – Lakshmi Mar 25 '16 at 09:06
  • Yes, I know the type of the string is `str`, that's the whole point about the `"" %` part. I still don't understand what the exact problem is: you want to show a value with two decimals, but don't want it to be a string. That makes no sense, since everything you show is converted to a string first. – Peter Mar 25 '16 at 09:08
  • yes.when i print out the value somewhere,it matters how many decimals numbers and datatype.i want two decimal value and that type is must be float – Lakshmi Mar 25 '16 at 09:14
  • You can only define how many decimals you print out at the moment you print them out. So you need to find where the value is printed out, and there apply the method is described in my answer. There is no other possibility, unless you seriously dig into the internals. – Peter Mar 25 '16 at 09:24
  • I added a method you could use, creating your own class. I would not advise using it unless you have no access to the part of the code that prints out the value. – Peter Mar 25 '16 at 09:32
  • Actually what i am doing reading price value from given url,take price value example '1,234.00' (this value is string)replace the comma and convert to float.then i got value(1234.0)this is also float.no issues.next i will take this float value to replace the one more meta filed.this meta field data type is float and it decimal value must be two example(1630.00).but i got only one decimal value – Lakshmi Mar 25 '16 at 09:35
  • You keep repeating that. I understand the problem. I've also explained that what you want is not straight-forward. See my last two comments. I can not help you more than this. – Peter Mar 25 '16 at 09:37