Python Forum
rounding locale.currency - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: rounding locale.currency (/thread-1350.html)



rounding locale.currency - birdieman - Dec-27-2016

I am new to python and barely know anything, but learned how to import "locale.currency" (from a suggestion to one of my other threads) to deal with formatting numbers into US currency.  The following code:

import locale

locale.setlocale(locale.LC_ALL, '')
'English_United States.1252'

locale.currency(xyz, grouping=True).rjust(14, ' ')
If xyz = 1234567.89, then this code prints

$1,234,567.89       

Does anyone know a way to alter the formula to ROUND (not truncate) to NO decimal places, to get:

$1,234,568

thanks








does what I want


RE: rounding locale.currency - Larz60+ - Dec-27-2016

import locale

xyz = 1234567.89

locale.setlocale(locale.LC_ALL, '')
'English_United States.1252'

rxyz = int(round(xyz))
print(locale.currency(rxyz, grouping=True).rjust(14, ' '))
results:
Output:
 $1,234,568.00



RE: rounding locale.currency - birdieman - Dec-27-2016

Larz

Thanks for the reply, but I do not want to show the .00

I just want to show $1,234,568


RE: rounding locale.currency - snippsat - Dec-27-2016

(Dec-27-2016, 01:52 PM)birdieman Wrote: I just want to show $1,234,568
>>> xyz = 1234567.89
>>> '${:,.0f}'.format(xyz)
'$1,234,568'



RE: rounding locale.currency - birdieman - Dec-27-2016

Thanks Snippsat

I think my problems formatting is due to how python reads numbers written to a file.

If I write integers or floating point numbers to a file, and then read them back, are they ALL strings?

If so, I gather that I must either (1) treat them as strings (like using ('$' + xyz), or (2) convert them somehow back to integers/floating point numbers before trying to print them using your example above?

Is this correct?

one last question. is there a way to add a comma (for thousands separator) to this format line?
'{}'.format('$' + begintaxableequity).rjust(12, ' ')



RE: rounding locale.currency - wavic - Dec-27-2016

It depends on what you want to achieve. If you have 5312.75 it will be 5313 ( if you round it ) and 5312 if you convert it to an integer or just cut off the decimal part when you format it.


RE: rounding locale.currency - Larz60+ - Dec-27-2016

For the record, You don't have to write data to a file as strings. Just add a mode
qualifier of 'b' when you open, and you can write data as binary.


The available modes are:

  • r for reading
  • w for writing
  • r+ opens for reading and writing (cannot truncate a file)
  • w+ for writing and reading (can truncate a file)
  • rb+ reading or writing a binary file
  • wb+ writing a binary file
  • a+ opens for appending



RE: rounding locale.currency - birdieman - Dec-27-2016

Thanks for the information on the various ways to read/write to a file.

I was wondering if there was a way to add a comma to the one line of code I posted on #5 above (I edited my original post so you probably did not see it)??

Thanks to all of you


RE: rounding locale.currency - birdieman - Dec-28-2016

I figured everything out -- thanks to all