0

When I load xlsx file. I have a number IBAN with scientific notation. For example:

7.810500161524e+25

This number should be:

78105001615240000000000000

I want to convert this number to string, but I get result:

'7.810500161524e+25'

I thought if I converted to int and then to string it would be the correct result, but again I get wrong result:

'78105001615239996483043328'

Anyone have any idea?

troleoaleo
  • 19
  • 2
  • Does this answer your question? https://stackoverflow.com/questions/25099626/convert-scientific-notation-to-float – Mortz Jul 29 '22 at 08:48
  • 1
    Shouldn't the Excel cell containing the IBAN be text, rather than a number, in the first place? – Thierry Lathuille Jul 29 '22 at 08:50
  • 2
    @Mortz: Converting to a float is actually counterproductive, since the float's handling of the digits beyond the precision of the scientific notation string will not be to just treat them as zeros. That's how you get `'78105001615239996483043328'` (or something like it). – Blckknght Jul 29 '22 at 09:16
  • @Blckknght - TY. Yeah, my bad. I misunderstood the question. – Mortz Jul 29 '22 at 09:23

1 Answers1

1

You can convert your string '7.810500161524e+25' to a decimal.Decimal number without altering its precision at all the way you will if you convert it to a floating point value. Then (if necessary) you can convert the decimal value to an integer or a string.

import decimal

scientific_notation = '7.810500161524e+25'
decimal_number = decimal.Decimal(scientific_notation) # Decimal('7.810500161524E+25')
int_number = int(decimal_number)                      # 78105001615240000000000000
fixed_point_string = format(decimal_number, 'f')      # '78105001615240000000000000'
Blckknght
  • 100,903
  • 11
  • 120
  • 169