formatNumber
String sutil.formatNumber ( String number ) (professional and enterprise editions only)
String sutil.formatNumber ( String number, int decimals, boolean padDecimals ) (professional and enterprise editions only)
String sutil.formatNumber ( String number, int decimals, boolean padDecimals ) (professional and enterprise editions only)
Description
Returns a number formatted in such a way that it could be parsed as a Float, such as xxxxxxxxx.xxxx. It attempts to figure out if the number is formatted as European or American style, but if it cannot determine which it is, it defaults to American. If the number is something with a k on the end, it will convert the k to thousand (as 000). It will also try to convert m for million and b for billion. It also assumes that you won't have a number like 3.123k or 3.765m, however 3.54m is fine. It figures if you wanted all three of those digits you would have specified it as 3765k or 3,765k
Parameters
- number String containing the number.
- decimals (optional) The number of maximum number of decimal places to include in the result. When this value is omitted, any decimals are retained, but none are added
- padDecimals (optional) Sets whether or not to pad the decimals (convert 5.1 to 5.10 if 2 decimals are specified)
Return Values
Returns a String formatted as a phone number, such as +1 (123) 456-7890x2, or null if the input was null
Change Log
Version | Description |
---|---|
5.5.26a | Available in all editions. |
Examples
Format a scraped abbreviated number as a dollar amount
// Format a number to two decimal places
String dollars = sutil.formatNumber("3.75k", 2, true);
// This would set dollars to the String "3750.00"
// Format the amount without cents.
String dollarsNoCents = sutil.formatNumber("3.75m");
// This would set dollars to the String "3750000"
String dollars = sutil.formatNumber("3.75k", 2, true);
// This would set dollars to the String "3750.00"
// Format the amount without cents.
String dollarsNoCents = sutil.formatNumber("3.75m");
// This would set dollars to the String "3750000"
Format a European number to be inserted in a MySQL statement
String number = sutil.formatNumber("3.275,10", 2, false);
// number would now be "3275.1"
// number would now be "3275.1"
mikes on 10/26/2011 at 5:01 pm
- Printer-friendly version
- Login or register to post comments