-3

In case

x = 0.898558 #float with zero prefix

I tried

from decimal import Decimal
x = 0.898558
print(Decimal(x))

Output:

0.898557999999999967855046634213067591190338134765625

I think i can do something and i tried

x = 0.898558
print('%.2f' % x)

Ouput:

0.89

How i can remove the 0 digit,i want something like this 89

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
alnyz
  • 91
  • 1
  • 1
  • 9
  • 1
    `0.89 * 100` is `89` – DeepSpace Dec 02 '18 at 17:19
  • Multiply `x` By 100 and use “%.0f” for formatting. – Craig Dec 02 '18 at 17:20
  • FYI the name `Decimal` does not mean that it only represents the "decimal part" of the number. The `Decimal` class simply uses a different representation to store floating point numbers which uses base 10 instead of base 2, and you can configure the precision. This representation should be used when you need better precision & rounding, for example in financial applications. – Bakuriu Dec 02 '18 at 17:22
  • soo thanks,its helpful – alnyz Dec 02 '18 at 17:22
  • 1
    Are you _sure_ your output is `0.89`? I'd expect `0.90`. – Mark Dickinson Dec 02 '18 at 17:25
  • on there as example sir – alnyz Dec 02 '18 at 17:31
  • When I, or apparently Mark Dickinson, execute `x = 0.898558` and `print('%.2f' % x)` in Python, the output is “0.90”. So we question whether you actually get “0.89”. You say it is an example, but what does that mean? An example of what you want? Or an example of what you get? For the following values of `x`, tell us exactly what output (the exact characters) you want: 1.25, .625, .375, −.375, .015625, 123, 123.5, 0, −3.125, 18446744073709551616, and 1e-30 (approximately). – Eric Postpischil Dec 03 '18 at 00:32

3 Answers3

0

You could do this by converting your int to a string and splitting by the decimal point.

x = str(x).split('.')[1]

kamses
  • 465
  • 2
  • 10
0

Its just a string, so you can slice off the bit you want:

print( ('%.2f' % x)[2:] )
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Multiply by 100, then convert to int:

y = int(x*100)

EDIT: This is WRONG. See discussion in the comments.

Yakov Dan
  • 2,157
  • 15
  • 29
  • This fails when `x` is 0.11999999999999999555910790149937383830547332763671875. It produces 12, but, given the OP’s example, the result should be 11. – Eric Postpischil Dec 03 '18 at 00:29
  • How do you even assign this value to a variable? I understand there's a finite precision issue here, but how do you replicate it? at least on my machine, x = immediately results in x = 0.12 – Yakov Dan Dec 03 '18 at 12:32
  • 1
    Assuming your Python implementation is using IEEE-754 basic 64-bit binary floating-point, you can assign the value simply with `x=0.11999999999999999555910790149937383830547332763671875`. It is exactly representable and so should be converted to floating-point properly. It does not immediately result in 0.12. Printing it with default formatting results in the actual value being converted to the string “0.12” for output, but the internal value remains the same. If your Python implementation has good formatting code, then `print "%.99g" % x` should show the exact value. – Eric Postpischil Dec 03 '18 at 15:05