source: code/Website/open-flash-chart/NumberUtils.as@ 7937

Last change on this file since 7937 was 7849, checked in by dennisw, 15 years ago
File size: 2.2 KB
Line 
1package {
2 public class NumberUtils {
3
4
5 public static function formatNumber (i:Number) : String{
6 var format:NumberFormat = NumberFormat.getInstance(null);
7 return NumberUtils.format (i,
8 format.numDecimals,
9 format.isFixedNumDecimalsForced,
10 format.isDecimalSeparatorComma,
11 format.isThousandSeparatorDisabled
12 );
13 }
14
15 public static function formatNumberY2 (i:Number) : String{
16 var format:NumberFormat = NumberFormat.getInstanceY2(null);
17 return NumberUtils.format (i,
18 format.numDecimals,
19 format.isFixedNumDecimalsForced,
20 format.isDecimalSeparatorComma,
21 format.isThousandSeparatorDisabled
22 );
23 }
24
25 public static function format(
26 i:Number,
27 numDecimals:Number,
28 isFixedNumDecimalsForced:Boolean,
29 isDecimalSeparatorComma:Boolean,
30 isThousandSeparatorDisabled:Boolean
31 ) : String {
32 if ( isNaN (numDecimals )) {
33 numDecimals = 4;
34 }
35
36 // round the number down to the number of
37 // decimals we want ( fixes the -1.11022302462516e-16 bug)
38 i = Math.round(i*Math.pow(10,numDecimals))/Math.pow(10,numDecimals);
39
40 var s:String = '';
41 var num:Array;
42 if( i<0 )
43 num = String(-i).split('.');
44 else
45 num = String(i).split('.');
46
47 //trace ("a: " + num[0] + ":" + num[1]);
48 var x:String = num[0];
49 var pos:Number=0;
50 var c:Number=0;
51 for(c=x.length-1;c>-1;c--)
52 {
53 if( pos%3==0 && s.length>0 )
54 {
55 s=','+s;
56 pos=0;
57 }
58 pos++;
59
60 s=x.substr(c,1)+s;
61 }
62 if( num[1] != undefined ) {
63 if (isFixedNumDecimalsForced){
64 num[1] += "0000000000000000";
65 }
66 s += '.'+ num[1].substr(0,numDecimals);
67 } else {
68 if (isFixedNumDecimalsForced && numDecimals>0){
69 num[1] = "0000000000000000";
70 s += '.'+ num[1].substr(0,numDecimals);
71 }
72
73 }
74
75 if( i<0 )
76 s = '-'+s;
77
78 if (isThousandSeparatorDisabled){
79 s=s.replace (",","");
80 }
81
82 if (isDecimalSeparatorComma) {
83 s = toDecimalSeperatorComma(s);
84 }
85 return s;
86 }
87
88 public static function toDecimalSeperatorComma (value:String) : String{
89 return value
90 .replace(".","|")
91 .replace(",",".")
92 .replace("|",",")
93 }
94
95 }
96}
Note: See TracBrowser for help on using the repository browser.