Ask Your Question
0

Digit Grouping

asked 2014-02-03 13:23:41 -0500

Hello,

New to Sage--thanks to the developers--and am using it to analysis some large numbers. Is there any way to have the resultant numbers be grouped for easy reading? Like 1,000,000 instead of 1000000?

Thank you.

edit retag flag offensive close merge delete

Comments

1

If you're dealing with integers, you can use `"{:,}".format(100000000)` (see http://stackoverflow.com/a/10742904/1401572).

John Palmieri gravatar imageJohn Palmieri ( 2014-02-04 06:13:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-02-03 21:22:15 -0500

You could define a pretty-printing function:

def print3(n):
    # add commas every third digit from the right
    s = ''
    for k, d in enumerate(str(n)[::-1]):
        if k%3 == 0: s += ','
        s += d
    print s[:0:-1]

and use it to print your numbers:

sage: print3(100)
100
sage: print3(1000)
1,000
sage: print3(10000)
10,000
sage: print3(100000000000)
100,000,000,000
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2014-02-03 13:23:41 -0500

Seen: 69 times

Last updated: Feb 03 '14