Ask Your Question
1

Digit Grouping

asked 2014-02-03 20:23:41 +0100

KChrisC gravatar image

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

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-02-04 13:13:51 +0100

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

edit flag offensive delete link more

Comments

John Palmieri gravatar imageJohn Palmieri ( 2014-02-04 13:15:28 +0100 )edit
0

answered 2014-02-04 04:22:15 +0100

slelievre gravatar image

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 20:23:41 +0100

Seen: 456 times

Last updated: Feb 04 '14