number-formatting

How to format a floating number to fixed width in Python

How to format a floating number to fixed width in Python Question: How do I format a floating number to a fixed width with the following requirements: Leading zero if n < 1 Add trailing decimal zero(s) to fill up fixed width Truncate decimal digits past fixed width Align all decimal points For example: % …

Total answers: 10

How to print +1 in Python, as +1 (with plus sign) instead of 1?

How to print +1 in Python, as +1 (with plus sign) instead of 1? Question: As mentioned in the title, how do I get Python to print out +1 instead of 1? score = +1 print score >> 1 I know -1 prints as -1 but how can I get positive values to print with …

Total answers: 4

Display a decimal in scientific notation

Display a decimal in scientific notation Question: How can I display Decimal(‘40800000000.00000000000000’) as ‘4.08E+10′? I’ve tried this: >>> ‘%E’ % Decimal(‘40800000000.00000000000000’) ‘4.080000E+10′ But it has those extra 0’s. Asked By: Greg || Source Answers: from decimal import Decimal ‘%.2E’ % Decimal(‘40800000000.00000000000000’) # returns ‘4.08E+10′ In your ‘40800000000.00000000000000’ there are many more significant zeros that have …

Total answers: 13

Add 'decimal-mark' thousands separators to a number

Add 'decimal-mark' thousands separators to a number Question: How do I format 1000000 to 1.000.000 in Python? where the ‘.’ is the decimal-mark thousands separator. Asked By: CarlosRibet || Source Answers: I didn’t really understand it; but here is what I understand: You want to convert 1123000 to 1,123,000. You can do that by using …

Total answers: 9

How to turn a float number like 293.4662543 into 293.47 in python?

How to turn a float number like 293.4662543 into 293.47 in python? Question: How to shorten the float result I got? I only need 2 digits after the dot. Sorry I really don’t know how to explain this better in English… Thanks Asked By: Shane || Source Answers: >>> print “%.2f” % 293.44612345 293.45 Answered …

Total answers: 8

What's the easiest way to add commas to an integer?

What's the easiest way to add commas to an integer? Question: Possible Duplicate: How to print number with commas as thousands separators? For example: >> print numberFormat(1234) >> 1,234 Or is there a built-in function in Python that does this? Asked By: ensnare || Source Answers: locale.format() Don’t forget to set the locale appropriately first. …

Total answers: 4

How do I format a number with a variable number of digits in Python?

How do I format a number with a variable number of digits in Python? Question: Say I wanted to display the number 123 with a variable number of padded zeroes on the front. For example, if I wanted to display it in 5 digits I would have digits = 5 giving me: 00123 If I …

Total answers: 7

How to print a number using commas as thousands separators

How to print a number using commas as thousands separators Question: How do I print an integer with commas as thousands separators? 1234567 ⟶ 1,234,567 It does not need to be locale-specific to decide between periods and commas. Asked By: Elias Zamaria || Source Answers: I’m sure there must be a standard library function for …

Total answers: 30