Output scientific with minimum two exponent digits in mpdecimal

somehow c-code printf by default? has a two+ digit format for the exponent. I can get similar in python by print('{:.2E}'.format(value)), but didn’t manage to apply in mpdecimal, despite mpdecimal claims to be python compatible in the format string for ‘mpd_format()’. Anyone here familiar enough with mpdecimal to point me to a solution?
TIA for any help.

1 Like

I don’t think it’s doable directly with mpdecimal API. It has a different convention for formatting exponent wrt the C standard, for example.

Either you can modify the returned string (add extra 0 to the exponent), or just format the value by yourself.

How?! Your example will just format a float or a Decimal with precision 2. But exponent formatting will depend on the value and it’s type. For floats - minimum 2 digits, for Decimal’s - 1.

Yes. It’s compatible in the fmt argument, it accepts same. But not in output wrt floats.

thank you, alas a little oddity in mpdecimal :frowning:

1 Like

It’s IBM spec, not the mpdecimal itself: “An exponent in character form is then suffixed to the converted coefficient (perhaps with inserted decimal point); this comprises the letter ‘E’ followed immediately by the adjusted exponent converted to a character form. The latter is in base ten, using the characters 0 through 9 with no leading zeros, always prefixed by a sign character (‘-’ if the calculated exponent is negative, ‘+’ otherwise).”

whow, I take my hat off to your extensive knowledge …

If there is none or only tedious option to format mpdecimal output to 2+ digits exponents, other way: is anyone aware of an option to format c-code printf output to scientific format without leading zeroes?
TIA for any help …

No, there is no such option. C standard says (e formatting type): “The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent.” Minimum one digits should be present with the a (hexadecimal) formatting type.

1 Like