Print Floating Point Values Without Leading Zero

Print floating point values without leading zero

As much as I like cute regex tricks, I think a straightforward function is the best way to do this:

def formatFloat(fmt, val):
ret = fmt % val
if ret.startswith("0."):
return ret[1:]
if ret.startswith("-0."):
return "-" + ret[2:]
return ret

>>> formatFloat("%.4f", .2)
'.2000'
>>> formatFloat("%.4f", -.2)
'-.2000'
>>> formatFloat("%.4f", -100.2)
'-100.2000'
>>> formatFloat("%.4f", 100.2)
'100.2000'

This has the benefit of being easy to understand, partially because startswith is a simple string match rather than a regex.

How to remove all leading zeros from decimals [closed]

Here is a simple way to do it using string manipulation:

# Your number, defined to 10 decimal places
x = 0.0000001230
# Convert to string using f-strings with 10 decimal precision
out = f'{x:.10f}'
# Split at the decimal and take the second element from that list
out = out.split('.')[1]
# Strip the zeros from the left side of your split decimal
out = out.lstrip('0')
out
>>> '1230'

As a one-liner:

out = f'{x:.10f}'.split('.')[1].lstrip('0')

If you want the final result to be an integer or a float, simply convert it after using int(out) or float(out).

Edit: if you want to change the precision (has to be fixed precision to account for trailing zeros), you just need to change the int appearing in the f-string: out = f'{x:.<precision>f}'.

Print leading zeros of a floating point number

For g, specify and width and precision:

>>> print "%02i,%02i,%05.3g" % (3, 4, 5.66)
03,04,05.66

f versus g

The difference between f and g is illustrated here:

>>> print "%07.1f, %07.1f, %07.1f" % (1.23, 4567.8, 9012345678.2)
00001.2, 04567.8, 9012345678.2
>>> print "%07.1g, %07.1g, %07.1g" % (1.23, 4567.8, 9012345678.2)
0000001, 005e+03, 009e+09

When given large numbers, g switches to scientific notation while f just uses more spaces.

Similarly, g switches to scientific notation, when needed, for small numbers:

>>> print "%07.1f, %07.1f, %07.1f" % (.01, .0001, .000001)
00000.0, 00000.0, 00000.0
>>> print "%07.1g, %07.1g, %07.1g" % (.01, .0001, .000001)
0000.01, 00.0001, 001e-06

Round a floating point number to n digits ignoring leading zeros and preserving them

Edit: for an unknown number of 0's I don't see a better alternative than

import re
some_float = 11.00001203456789
how_many_to_keep = 4
regex = r"0[1-9]"
float_part = str(some_float).split(".")[1]
float_limiter = re.search(regex,float_part).start() if float_part.startswith("0") else -1
print(f'{some_float:2.{float_limiter+1+how_many_to_keep}f}')

Which is evaluating the number of 0 in real time using the expression float_limiter+1+how_many_to_keep, and use as a the format parameter. this has gotten a bit ugly but now returns the correct answer on all cases. The output is

11.00001235

golang printf float no leading zero

python format a floating number to print .123 not 0.123

I do not think there is a format string which removes the zero. However, you could use lstrip:

In [25]: n = 0.12345
In [26]: '{:.3f}'.format(n).lstrip('0')
Out[26]: '.123'

At least that is safer than str(n)[1:], which would remove a signficiant digit if n were equal to a number bigger than 1 or less than -1.

How can I print the integer part of a floating point number with 2 digits using leading zeros in Rust?

https://doc.rust-lang.org/std/fmt/#width

fn main() {
println!("{:04.1}%", 5.0);
}

prints

05.0%

which is padded to a length of 4 characters including the decimal point. Your example would be padded to length 2, but already has length 3, thus nothing changes.



Related Topics



Leave a reply



Submit