ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Add Commas to a Number
Submitter: Keith Vetter (other recipes)
Last Updated: 2001/09/09
Version no: 1.0
Category: Text Processing

 

5 stars 3 vote(s)


Approved

Description:

Takes a number and inserts a comma (or separator of your choice) between
every third digit.

Source: Text Source

proc comma {num {sep ,}} {
    while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
    return $num
}

The license for this recipe is available here.

Discussion:

Numbers displayed with the appropriate commas are much easier for a
person to read. This code is based on an example from the Perl documentation.



Add comment

No comments.



Highest rated recipes:

1. Get widget info

2. With busy cursor

3. Supporting mouse wheel ...

4. Multi-character split

5. LCD Number Display

6. Check creditcard numbers ...

7. WSCP - showServerStatus

8. A minimal debugger

9. Socket based communicatio...

10. Inline GIF image into ...




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.