1

The following code prints decimals of various precisions, but reverts to scientific notation past 7 places. I need to display a string width of at least 8 places in my UI.

How do I get a stringified decimal with 8 places of precision?

from decimal import *
getcontext().prec = 8 # set precision to 8 decimal points. 
getcontext().rounding = "ROUND_DOWN" # alway round down 

# stringified zero of various precisions

zw = ['0', '0.0', '0.00', '0.000000', '.00000000', '0.00000000', '0.000000000000']

for n in range(0,len(zw)): 
    zstr = zw[n]                # stringified zero 
    zdec = Decimal(zstr)        # decimalized zero
    print (zstr, ":", zdec) # compare stringified and decimalized zero
James Aanderson
  • 320
  • 2
  • 10

2 Answers2

1

Use format/f and string formatting (docs):

for zstr in zw:   
    zdec = Decimal(zstr)
    print (zstr, ":", f'{zdec:.8f}') 

0 : 0.00000000
0.0 : 0.00000000
0.00 : 0.00000000
0.000000 : 0.00000000
.00000000 : 0.00000000
0.00000000 : 0.00000000
0.000000000000 : 0.00000000
andrew_reece
  • 20,390
  • 3
  • 33
  • 58
1

Try:

from decimal import *
getcontext().prec = 8 # set precision to 8 decimal points. 
getcontext().rounding = "ROUND_DOWN" # alway round down 

# stringified zero of various precisions

zw = ['0', '0.0', '0.00', '0.000000', '.00000000', '0.00000000', '0.000000000000']

for zstr in zw: 
    zdec = Decimal(zstr)        # decimalized zero
    print ('{} : {:.8f}'.format(zstr, zdec)) # compare stringified and decimalized zero

Prints:

0 : 0.00000000
0.0 : 0.00000000
0.00 : 0.00000000
0.000000 : 0.00000000
.00000000 : 0.00000000
0.00000000 : 0.00000000
0.000000000000 : 0.00000000
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thanks! Is there a chart somewhere of the primitives and their respetive methods? I'm coming from perl and I'm having a hard time sanity checking data due to the difference in method availability. In perl I used to test for len() of things all the time, but Python, None and False don't have a len() method. (in perl, either would be 0) – James Aanderson Jul 28 '18 at 16:17
  • The spec for the [.format mini language](https://docs.python.org/3/library/string.html#formatspec) which is a `.str` method. The Python docs on the main [built-in types](https://docs.python.org/3/library/stdtypes.html#built-in-types) has a list of their methods. – dawg Jul 28 '18 at 18:26
  • 1
    One big thing to get over quickly in Python vs Perl (this will bite you) is the difference between what [Python thinks is Truthy](https://docs.python.org/2.4/lib/truth.html) and [Perl thinks is Thruthy](http://www.sthomas.net/roberts-perl-tutorial.htm/ch5/The_Truth_According_to_Perl). It is easier in Python but the Perl idioms are hard to forget. :-) – dawg Jul 28 '18 at 18:27
  • I'm still not really seeing a clean way of dealing with user input that may be a stringified zero. If I check len() thats fine, but then to do math I have to change the type. And if my function also returns zero, then I have to change it back to a string, and check len() again to determine whether the data is worth rendering back to the user. That is about 4 lines of code to do what len() or exists() does in perl. – James Aanderson Jul 28 '18 at 19:00
  • 1
    The Perly / Awky way of treating a string as a number waiting for arithmetic on it does not work in Python. The best way is to use `try / except` with a conversion to `int(user_input)` or a regex or Python string function to validate you have nothing but digits then use `int()` or `float()` on that string. To paraphrase the Camel Book: *You will be miserable until you learn the difference between different object types in Python.* – dawg Jul 28 '18 at 22:36
  • I think I get it now. If your going to do anything math, you create a class with polymorphic inputs and then use pythons operator overloading __str__ to stringify the output. This also of course means you can/must operator overload + / - etc. to get type ambivolence in the higher level code. Wow that is a lot of work just so that Python can regard a stringified zero, as False without evaluating it. – James Aanderson Jul 29 '18 at 16:00