-4

I have some floating point values, these all values are dynamically generated:

float_a = 12.200
float_b = 14.0
float_c = 14.01880

I want to remove the decimal point from them.

Expected Result

"12200"
"140"
"1401880"

I must be careful as these are not fixed-point values and I don't have any control on how many decimal places my input will go up to. More examples from the comments:

  1. 1.10
  2. 1.110
  3. 2.0
  4. 2.1340

Expected result:

  1. "110"
  2. "1110"
  3. "20"
  4. "21340"
Will
  • 24,082
  • 14
  • 97
  • 108
TidyWay
  • 84
  • 1
  • 1
  • 9
  • Its not a fixed values like a = 1.10, b= 1.110, c = 2.0,d = 2.1340, result i need a = "110", b = "1110", c = "20", d = "21340" – TidyWay May 12 '15 at 04:46
  • 4
    Wherever this value comes from, it doesn't start out as a float. You need to avoid turning it into a float, because `12.200` and `12.2` are going to evaluate to identical floats. – user2357112 May 12 '15 at 04:53
  • 2
    There is no way to differentiate what value was use to set `a` to `12.2` if it is truly a float. ie, you cannot tell after the fact if `a=12.2` or `a=12.200` which seems to be the premise of this question. –  May 12 '15 at 05:32

5 Answers5

3

This question seems to be a permutation of another question that has already been answered:

The concept of leading zeros is a display concept, not a numerical one. You can put an infinite number of leading zeros on a number without changing its value. Since it's not a numeric concept, it's not stored with the number.

You have to decide how many zeros you want when you convert the number to a string. You could keep that number separately if you want.

SOURCE: Python Force python to keep leading zeros of int variables

It looks like the only way to do what you are asking is if your initial "Float" is in string form, otherwise the trailing 0's will be dropped. If you manage to get the "Float" as a string (before it ever becoming a Float), then you can use the int('12.200'.replace('.', '')) method mentioned above

Community
  • 1
  • 1
Hybrid
  • 6,741
  • 3
  • 25
  • 45
  • hello hybrid, in this case we cant decide how many zeros will be put. – TidyWay May 12 '15 at 05:00
  • I understand, but my point is that it seems that since that is the case, it is not possible to do what you are asking, unless the initial value is in `string` form. The second it is converted into a `float`, the trailing 0's are removed. – Hybrid May 12 '15 at 05:01
  • @TidyWay unfortunately this is float values do not store ptecission values. use something else like Decimal for example. – joojaa May 12 '15 at 05:08
  • Thanks hybrid, @ joojaa can you give me example with same? – TidyWay May 12 '15 at 05:10
2

If you're starting off with a string, which I think you are. I would use something like:

>>> int('12.200'.replace('.',''))
12200

It just removes the . and parses the resulting string as an int. Otherwise just cast whatever you have to str first.

spalac24
  • 1,076
  • 1
  • 7
  • 16
  • Its not a string, its a float value – TidyWay May 12 '15 at 04:52
  • 3
    "Otherwise just cast whatever you have to str first." You cannot cast it to string first. The trailing zero is dropped if it's originally a float. – Martin Konecny May 12 '15 at 04:52
  • If you have those floats in your code, then just delete the dots in them. You cannot place thousand separator in python code. Or at least you should not. Otherwise, how are you getting them? – spalac24 May 12 '15 at 04:53
  • @MartinKonecny well... I was talking pretty much before he did the assignment as float. After that yes, the zeroes cannot be retrieved. – spalac24 May 12 '15 at 04:54
  • its a configuration part, so user will decide, some time it will be set 2 decimal some user will set 3 decimal, so i want to make a generic value to generate barcode from amount total :) so if user set 2 decimal then value comes with 2 decimal points same for 3 or 4 decimal. – TidyWay May 12 '15 at 04:57
  • 1
    How is this configuration read? Is it directly at the code to be edited by the "user"? If so, it sounds awfully wrong in my opinion. Maybe have it read from a separate file and parse it correctly (like in my answer). Or, if it has to be in the code, what's the problem of surrounding them with `"`? Instead of `a = 12.000` you would have `a = "12.200"` and then do the rest... – spalac24 May 12 '15 at 04:59
  • Its whole framwork, i read the float value from orders(because we defined field as float), so value will retrieve as float instead of string. – TidyWay May 12 '15 at 05:04
  • 1
    You need to be more clear about this. Put a full example of how the field is retrieved. What do you mean by "I read the float value from orders"? – spalac24 May 12 '15 at 05:10
1

It is just a formatting issue. Either of these work to get trailing zeroes:

>>> '{:.3f}'.format(1.2)
'1.200'
>>> '%0.3f' % (1.2,)
'1.200'

Then:

>>> '{:.3f}'.format(12.2).replace('.','')
'12200'

To get what you want from your examples:

vals=(1.10, 1.110, 2.0, 2.134)

def wid(f):
    w=1
    while True:
        s='{:.{w}f}'.format(f, w=w)
        if s[-1]=='0' or w>5:
            break
        w+=1    
    return w

for e in vals:
    s='{:.{w}f}'.format(e, w=wid(e)).replace('.','')
    print '{:10} => {}'.format(e, s)

Prints:

   1.1 => 110
  1.11 => 1110
   2.0 => 20
 2.134 => 21340
dawg
  • 98,345
  • 23
  • 131
  • 206
0

If you want to remove the decimal, why not multiply by 1000?

float_a = 12.200
print str(int(float_a*1000))

When you have

float_a = 1.110
float_b = 1.1100
float_c = 1.11000

Python is discarding the trailing zeros, and the above 3 are the exact same. These trailing zeros are non-significant digits, and so if you want to actually preserve them, your original data type will need to be a String.

If you make that change, then it is simple to do

float_a = "1.110"
print float_a.replace('.', '') #  "1110"
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Your first solution was better, since OP is not using a fixed number. – Hybrid May 12 '15 at 04:47
  • The only issue is that if the original value is a float, I can't know how many trailing zeros the string should have. – Martin Konecny May 12 '15 at 04:48
  • 1
    ts not a fixed values like a = 1.10, b= 1.110, c = 2.0,d = 2.1340, result i need a = "110", b = "1110", c = "20", d = "21340", how can i know it will be multiply by 1000 :) sometime values comes in 2 decimal also. – TidyWay May 12 '15 at 04:49
0

If you're asking how to turn float_a = 12.200 into the string "12200", simply multiply it by 1000 before converting it into a string, for example:

print(str(float_a * 1000))

However, if the number contains more than 4 decimal places, you'll still have decimals.

However, since you're talking about it "always removing the 0's", I suspect you may have missed a . in your expected output. In this case, to display a float with a fixed number of decimals, just use Python's built-in string formatting:

float_a = 12.200
expected_result = '%.2f' % float_a

print expected_result

> 12.20

If this doesn't make sense, please clarify your question a bit and I'll edit! :)

Edit

Upon your further clarification of the question, it seems that you want to define a float with trailing zeros and have them preserved and the float converted to a string with the leading zeros preserved. I'm afraid this is impossible if your input type is float, since float won't preserve those trailing zeros to begin with:

In [1]: float_a = 3.1400

In [2]: float_a Out[2]: 3.14

This is a "limitation" of the data type and there's nothing you can do about it. You want a fixed-point type or something to handle "decimal numbers", not a float.

Will
  • 24,082
  • 14
  • 97
  • 108