Display a Decimal in Scientific Notation

Display a decimal in scientific notation

from decimal import Decimal

'%.2E' % Decimal('40800000000.00000000000000')

# returns '4.08E+10'

In your '40800000000.00000000000000' there are many more significant zeros that have the same meaning as any other digit. That's why you have to tell explicitly where you want to stop.

If you want to remove all trailing zeros automatically, you can try:

def format_e(n):
a = '%E' % n
return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]

format_e(Decimal('40800000000.00000000000000'))
# '4.08E+10'

format_e(Decimal('40000000000.00000000000000'))
# '4E+10'

format_e(Decimal('40812300000.00000000000000'))
# '4.08123E+10'

How to print decimal notation rather than scientific notation in Python?

p = 0.0000000000000000000001

s = str(p)

print(format(p, "." + s.split("e")[-1][1:]+"f")) if "e" in s else print(p)

Python decimal.Decimal producing result in scientific notation

Render the decimal into a formatted string with a float type-indicator {:,f}, and it will display just the right number of digits to express the whole number, regardless of whether it is a very large integer or a very large decimal.

>>> val
Decimal('1000000000000000000000000')

>>> units
Decimal('1500000000')

>>> "{:,f}".format(units / val)
'0.0000000000000015'

# very large decimal integer, formatted as float-type string, appears without any decimal places at all when it has none! Nice!

>>> "{:,f}".format(units * val)
'1,500,000,000,000,000,000,000,000,000,000,000'

You don't need to specify the decimal places. It will display only as many as required to express the number, omitting that trail of useless zeros that appear after the final decimal digit when the decimal is shorter than a fixed format width. And you don't get any decimal places if the number has no fraction part.

Very large numbers are therefore accommodated without having to second guess how large they will be. And you don't have to second guess whether they will be have decimal places either.

Any specified thousands separator {:,f} will likewise only have effect if it turns out that the number is a large integer instead of a long decimal.

Proviso

Decimal(), however, has this idea of significant places, by which it will add trailing zeros if it thinks you want them.

The idea is that it intelligently handles situations where you might be dealing with currency digits such as £ 10.15. To use the example from the documentation:

>>> decimal.Decimal('1.30') +  decimal.Decimal('1.20')
Decimal('2.50')

It makes no difference if you format the Decimal() - you still get the trailing zero if the Decimal() deems it to be significant:

>>> "{:,f}".format( decimal.Decimal('1.30') +  decimal.Decimal('1.20'))
'2.50'

The same thing happens (perhaps for some good reason?) when you treat thousands and fractions together:

>>> decimal.Decimal(2500) * decimal.Decimal('0.001')
Decimal('2.500')

Remove significant trailing zeros with the Decimal().normalize() method:

>>> (2500 * decimal.Decimal('0.001')).normalize()
Decimal('2.5')

Convert scientific notation to decimals

I'm making this answer since the top voted one has misinformation and so i can explain my improvements.

TL;DR: Use ("%.17f" % n).rstrip('0').rstrip('.')


By default Python formats to scientific notation if there's 5 or more zeroes at the beginning.

0.00001 / 1e-05 formats to "1e-05".

0.0001 / 1e-04 formats to "0.0001".

So of course 8.99284722486562e-02 will format to "0.0899284722486562" already.

A better example would've been 8.99284722486562e-05. (0.00008992847224866)

We can easily format to raw decimal places with "%f" which is same as "%.6f" by default.

"%f" % 8.99284722486562e-05 produces '0.000090'.

"%f" % 0.01 produces '0.010000'.


By default floats display upto 17 decimal places.

0.1234567898765432123 - (19 dp input)

0.12345678987654321 - (17 dp output)

So if we did "%.17f" % 8.99284722486562e-02 we'd get '0.08992847224865620'. (note the extra 0)

But if we did "%.17f" % 0.0001 we surely wouldn't want '0.00010000000000000'.

So to remove the trailing zeroes we can do: ("%.17f" % n).rstrip('0').rstrip('.')

(Notice we also strip the decimal point incase the number has no fraction left)


Also there's counterparts to %f:

%f shows standard notation

%e shows scientific notation

%g shows default (scientific if 5 or more zeroes)

Forcing R output to be scientific notation with at most two decimals

I think it would probably be best to use formatC rather than change global settings.

For your case, it could be:

numb <- c(0.05, 0.05671, 0.000000027)
formatC(numb, format = "e", digits = 2)

Which yields:

[1] "5.00e-02" "5.67e-02" "2.70e-08"

Format decimal to number of decimal places without scientific notation

You can try like this:

0.1234567.ToString("0.####")

Also check Custom Numeric Format Strings

#

Replaces the "#" symbol with the corresponding digit if one is
present; otherwise, no digit appears in the result string.

Also as Jon as correctly pointed that it will round your number. See the note section

Rounding and Fixed-Point Format Strings

For fixed-point format strings
(that is, format strings that do not contain scientific notation
format characters), numbers are rounded to as many decimal places as
there are digit placeholders to the right of the decimal point.



Related Topics



Leave a reply



Submit