I'm familiar with the convention of using %.2f
to set two decimal places for a float but is it in any way possible to change the number to a variable so that the user can state the number of decimal places displayed?
At this time, the Decimal data type can only be used within a Variant; that is, you cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal by using the CDec function.
From Python 3.2 onwards, a Decimal instance can also be constructed directly from a float .
.format
is a nicer, more readable way to handle variable formatting:
'{:.{prec}f}'.format(26.034, prec=3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With