0

Seen some other questions but none have answered this particular case

val = "0.000"
print val
0.000
f = float(val)
print f
0.0

How can I keep these as floats but force them to be the exact value of 0.000?

7
  • Isn't 0.0 same as 0.0000? Some other example would have been better Commented Jan 24, 2015 at 18:18
  • No, 0.000 represents 0 down to 10^-4 Commented Jan 24, 2015 at 18:18
  • @Badmiral but 0.0 == 0.0000 = True Commented Jan 24, 2015 at 18:20
  • docs.python.org/2/library/decimal.html "The decimal module incorporates a notion of significant places" Commented Jan 24, 2015 at 18:21
  • 1
    @MartijnPieters if OP is doing for example physics stuff, there's a difference between 0.0 and 0.00 Commented Jan 24, 2015 at 18:23

1 Answer 1

4

The float values 0.0 and 0.000 are indistinguishable from one another: they are represented by exactly the same bit pattern.

You'd have to use a different data type -- such as a string or Decimal -- if you want to preserve the zeroes:

In [7]: from decimal import Decimal

In [8]: d1 = Decimal("0.0")

In [9]: d2 = Decimal("0.000")

In [10]: str(d1)
Out[10]: '0.0'

In [11]: str(d2)
Out[11]: '0.000'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.