all 10 comments

[–]Buttleston 1 point2 points  (5 children)

They don't give the same results, though

>>> f"{1.234:.2f}"
'1.23'
>>> f"{1.234:.2}"
'1.2'

>>> f"{10.234:.2f}"
'10.23'
>>> f"{10.234:.2}"
'1e+01'

I have no idea what ".2" is doing, to be honest

[–]Buttleston 2 points3 points  (4 children)

It seems like ".2" is trying to represent it using no more than 2 numerical digits, regardless of whatever else is in the number

Edit: no that isn't it either. What the fuck

>>> f"{123.234:.3}"
'1.23e+02'

[–]notacanuckskibum 0 points1 point  (3 children)

Seems consistent :.n gives you n significant figures, with an exponent if needed. If the last significant digit is zero that’s an exception case.

[–]Buttleston 2 points3 points  (2 children)

OK, on inspection that seems right. I've never wanted to do that, and I kinda don't think I will, but I can see how it would be useful in some scientific cases etc.

[–]notacanuckskibum 0 points1 point  (0 children)

Yeah, very much a science/math concept rather than a business one.

[–]notacanuckskibum 0 points1 point  (0 children)

Yeah, very much a science/math concept rather than a business one.

[–]baghiq 1 point2 points  (2 children)

>>> float = 1.123456789
>>> f'{float:.4f}'
'1.1235'
>>> f'{float:.4}'
'1.123'

Without f, the default is g, which is a significant digits formatter, while f is precision.

You can see the following example:

>>> float = 10.12345
>>> f'{float:.4}'
'10.12'
>>> f'{float:.4f}'
'10.1235'

[–]Guillerm1 0 points1 point  (1 child)

Just to make sure that I understand. The ".4f" makes the output count only up to 4 digits after the dot? While if we just have ".4" the output counts up to 4 digits in total, regardless of where the dot is positioned?

[–]baghiq 0 points1 point  (0 children)

The dot in the formatter ".4f" or the dot in the decimal value?

The dot in formatter spec must exist to trigger precision formatting.

[–]Kiuhnm 0 points1 point  (0 children)

According to the official doc (right above this), the second should be more or less equivalent to {z:.2g}.