Home > Back-end >  Add commas to decimal column without rounding off
Add commas to decimal column without rounding off

Time:01-23

I have pandas column named Price_col. which look like this.

       Price_col
    1. 1000000.000
    2. 234556.678900
    3. 2345.00
    4.
    5. 23.56

I am trying to add commas to my Price_col to look like this.

       Price_col
    1. 1,000,000.000
    2. 234,556.678900
    3. 2,345.00
    4.
    5. 23.56

when I try convert the values it always round off. is there way that I can have original value without rounding off.

I tried below code. this what I got for the value 234556.678900.

n = "{:,}".format(234556.678900)
print(n)
>>> 234,556.6789

CodePudding user response:

Add f for fixed-point

>>> "{:,}".format(234556.678900)
'234,556.6789'
>>> "{:,f}".format(234556.678900)
'234,556.678900'

You can also control the precision with .p where p is the number of digits (and should probably do so) .. beware, as you're dealing with floats, you'll have some IEEE 754 aliasing, though representation via format should be quite nice regardless of the backing data

>>> "{:,.5f}".format(234556.678900)
'234,556.67890'
>>> "{:,.20f}".format(234556.678900)
'234,556.67889999999897554517'

The full Format Specification Mini-Language can be found here:
https://docs.python.org/3/library/string.html#format-specification-mini-language

  •  Tags:  
  • Related