Currency format

This is the place for queries that don't fit in any of the other categories.

Currency format

Postby pythonsunu » Wed Oct 02, 2013 10:31 am

I would like to format currency from various locale to one standard form, please help:

Ex: Input
123,345,456,44 or
123.345.456.44

Expected Result : 123,345,456.44

I can match a pattern but replacing only a part of the pattern is what I am struggling. Any help is greatly appreciated.

Thanks,
pythonsunu
 
Posts: 4
Joined: Wed Oct 02, 2013 10:24 am

Re: Currency format

Postby metulburr » Wed Oct 02, 2013 11:14 am

I'm trying to think if there is a way to do this without converting to a string first.

the best way i can think of is format(), but this is starting no commas in the first place

Code: Select all
>>> "{:,}".format(123456789)
'123,456,789'
>>> "{:,}".format(1234567891)
'1,234,567,891'
>>> "{:,}".format(1234567891.41)
'1,234,567,891.41'
>>>


However if your starting with commas, you will have to result to str.replace(), however you are going to have to finagle it to have it do what you are expecting.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Currency format

Postby pythonsunu » Wed Oct 02, 2013 11:21 am

Thanks for the reply, but I am looking for replacing "," or "." in the numbers.

like for ex:

Input = 123,123,12
output =123,123.12 (with a "." before the last 2 digits)

and

Input = 234.234.23
output = 234,234.23
pythonsunu
 
Posts: 4
Joined: Wed Oct 02, 2013 10:24 am

Re: Currency format

Postby metulburr » Wed Oct 02, 2013 11:31 am

Is this a school project? If it is, then it makes sense just for the exercise. On the second one, you can just remove the comma and replace it with an empty string, and throw it into format() to output it as expected. The first one however, you are going to have to identify the last comma as replacing to a period and not an empty string.

If it is not a school project, then no one would save their data in this manner. It is normally input as a float or int in string form which then can be converted with currency format on the standard output. Change your program around to input/save the data without the commas.

EDIT:
Code: Select all
input1 = "123,123,12"
input2 = "123.123.12"

count1 = input1.count(',')-1
count2 = input2.count(',')-1

input1_replaced = float(input1.replace(',','', count1).replace(',','.'))
input2_replaced = float(input2.replace('.','', count2))

print("{:,}".format(input1_replaced))
print("{:,}".format(input2_replaced))


There probably is a better way, but like i said, its better to of not accepted input, or saved the commas in the first place.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Currency format

Postby pythonsunu » Wed Oct 02, 2013 11:50 am

Thanks that works!

I was trying something like this, I am not very good at regular expressions!
====================================
import re

format1 = "123.123.12"

num1 = re.sub(r'(.)(\d{3})',',\1',format1)
print(num1)
======================================
The output is :123, .12 (which is wrong). Is there a way I can do it using the regular expression above?
pythonsunu
 
Posts: 4
Joined: Wed Oct 02, 2013 10:24 am

Re: Currency format

Postby metulburr » Wed Oct 02, 2013 11:58 am

I am not very good at regular expressions!

here too

You would need someone else for regex, as i can just barley do basics with it. However regex goes against pythonic style, specifically the readability. Also, In the time it would take you to write out a working regex, you could of written out 5 times the code using python's built-ins and string methods, if not more.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests