Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rounding locale.currency
#1
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
Reply
#2
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
Reply
#3
Larz

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

I just want to show $1,234,568
Reply
#4
(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'
Reply
#5
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, ' ')
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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
Reply
#8
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
Reply
#9
I figured everything out -- thanks to all
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 711 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  need help rounding joseph202020 7 1,324 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  from numpy array to csv - rounding SchroedingersLion 6 2,176 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  xml extract currency 3lnyn0 5 1,685 Apr-30-2022, 10:29 AM
Last Post: snippsat
  Random data generation sum to 1 by rounding juniorcoder 9 3,430 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,421 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,564 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 2,764 Jan-06-2021, 03:14 PM
Last Post: project_science
  rounding and floats Than999 2 3,104 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,089 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020