You'll need to use string formatting for this:
'{:0.3e}'.format(2.32432432423e25)
The reason is that round is for specifying the number of the digits after the ones place, which is not really relevant when your numbers are O(25).
You'll need to use string formatting for this:
'{:0.3e}'.format(2.32432432423e25)
The reason is that round is for specifying the number of the digits after the ones place, which is not really relevant when your numbers are O(25).
If you want to use Python's f-string syntax introduced in Python 3.6, specify the format after the variable, separated by :, e.g.:
>>> res = 2.32432432423e25
>>> f'The result is {res:.3e}'
'The result is 2.324e+25'
I have a simple set of needs for a function:
round values to a specified number of significant figures
or
round values to a specified decimal place (e.g. round to nearest 0.01)
in either case it must:
print the values explicitly not in scientific notation
print the values with their proper number of significant digits without truncating trailing zeroes
These should be simple requirements that have been done many times before but i cannot find any solution to this issue outside of doing it manually by hand. It does not need to all be one library, i will take literally any mishmash of code that works and use the sig fig rounding and decimal place rounding separately if need be. I will list below the solutions I've come across and examples where they don't work:
round() does not work because it truncates trailing zeroes, variations using the round() function with floor,log10 and so on do not remedy this. It only rounds to decimal places, it cannot round to significant figures.
round(1234.000000,3) #1234.0 #not 1234.000
numpy.format_float_positional does not work because of how it handles floating point arithmetic i believe:
np.format_float_positional(0.0900,3,fractional=False,unique=False) #'0.09' np.format_float_positional(0.0800,3,fractional=False,unique=False) #'0.0800'
numpy.format_float_scientific does not work because it prints as an exponent, any attempt to print it explicitly results in trailing zeroes being truncated:
np.format_float_scientific(0.9000000,3,unique=False) #'9.000e-01' #which is fine but cannot be converted out of exponent form in any way without significant figures being lost
decimal.Decimal() does not work because it can only handle the set precision rounding, i can't see anyway to use it to round to a set number of significant figures and it does not interplay with numpy properly:
np.format_float_positional(Decimal.from_float(0.7),4,fractional=False,unique=False)
#'0.700'
#not '0.7000'
np.format_float_positional(Decimal.from_float(0.8),4,fractional=False,unique=False)
#'0.8000'
#as you can see decimal doesn't work for significant figures because i believe numpy imports the value
#and doesn't preserve the values in the way the Decimal library intends.
#Decimal('0.7') has similar issuesdecimal.Decimal().quantize(decimal.Decimal()) does work but only for rounding to a set decimal place, it won't work for rounding to the nearest 10 or 100 as far as i can get it to work and I cannot find a way to round to a specified number of significant figures using the decimal library.
print(str(Decimal('.900000').quantize(Decimal('0.01'))))
#0.90
print(str(Decimal('93256').quantize(Decimal('100'))))
#93256
#not 93200ideally i was thinking of a function with the syntax:
def round_value(value,number_of_significant_figures=None,decimal_precision=None): #when either of the kwargs is set to a number it would round based on the number of specified significant figure or to the specified decimal place as appropriate
I have tried varying combinations of the above functions/libraries with little success, inevitably one of the three requirements is violated by some reasonable number. If someone can demonstrate/direct me to a reliable way to round values meeting the listed criteria it would be immensely appreciated because i am at my wits end.