473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Concatenated numerical literals

Hi,

I wish I could write large integers (and floats, etc) like
this in Python:

10 000 000

Kind of the same way I can write string literals today.
(As in 'This' 'is' 'one' 'string'.)

Has there been any discussion on this? I find myself wishing
for this at least once a week, since it really helps readability.
It doesn't seem like a big implementation either.

Any comments?

Regards,
Per Erik Stendahl,
Uppsala, Sweden

Jul 18 '05 #1
1 1264
I took a function that I wrote to format floats with
commas and currency symbol and hacked it a little to make
it work with your spaces. You should be able to take it
and with little modifications make it work for you.

-Larry

def fmt_currency(amount):
import types, string
'''
This function will take the number passed to it and format it as US
Currency with floating $ sign, comma separators and two decimal places
'''
thousands_sep=" "
currency="$"
#
# Check to see if I got a number or a string
#
if type(amount) == types.StringType:
#
# Check to see if I got an empty string, return $0.00
#
if amount == '': amount=0
else: amount=string.atof(amount)

temp="%.2f" % amount
outstring=temp[len(temp)-4:]
if len(temp)==4: return currency+outstring
digits=list(temp[0:len(temp)-4])
digits.reverse()
#print digits
cpos=1
for c in digits:
outstring=c+outstring
cpos=cpos+1
if cpos%3 == 0: outstring=thousands_sep+outstring
#
# Check to see if comma is first character of the formatted string
# if it is strip it off.
#
if outstring.startswith(thousands_sep): outstring=outstring[1:]
return currency+outstring

if __name__=="__main__":
print fmt_currency(0.12)
print fmt_currency(123.45)
print fmt_currency(123456789.01)
"Per Erik Stendahl" <be***@psyket.com> wrote in message
news:ma*************************************@pytho n.org...
Hi,

I wish I could write large integers (and floats, etc) like
this in Python:

10 000 000

Kind of the same way I can write string literals today.
(As in 'This' 'is' 'one' 'string'.)

Has there been any discussion on this? I find myself wishing
for this at least once a week, since it really helps readability.
It doesn't seem like a big implementation either.

Any comments?

Regards,
Per Erik Stendahl,
Uppsala, Sweden

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rob Miller | last post by:
A new book with a numerical library in Java was published in August 2003. Title: "A Numerical Library in Java for Scientists and Engineers" Author: Hang T. Lau ISBN: 1584884304 Published date:...
20
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using...
7
by: M Wells | last post by:
Hi All, I have what seems to me to be a difficult query request for a database I've inherited. I have a table that has a varchar(2000) column that is used to store system and user messages...
2
by: B Love | last post by:
Hello Group, I have 2 text fields that I would like to concatenate for use in a table. One field is an ordinary text box. The other is a simple combo box which I use to select one of about ten...
6
by: copx | last post by:
Can you / are you supposed to free() string literals which are no longer needed? In my case I've menu construction code that looks like this: menu_items = list_new(); list_add(menu_items,...
4
by: Kurt | last post by:
I'm using the fConcatChild function posted at http://www.mvps.org/access/modules/mdl0004.htm to return a field from the Many table of a 1:M relationship into a concatenated string. The function...
23
by: Abhi | last post by:
Hi.. I wanted the C source code in machine readable format for the book "Numerical Recipes in C". I got hold of the pdf version of the book somehow. Does anyone have the complete C code of the...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
10
by: Babak | last post by:
Hi, I've developed a C program which contains a large number of vectors and matrices operations. Throughout my code, I used the template from the Numerical Recipes book to define vectors and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.