-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWS-formatNumber.js
171 lines (154 loc) · 4.15 KB
/
WS-formatNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*function : format_number()
version: 1.0.0
This function formats a numeric value passed in to it with specified number of
decimal values. numeric value will not be rounded.
pnumber : numeric value to be formatted.
decimals : number of decimal points desired.
Author: Buddhike de Silva
Date: 21-Nov-2002 11:16 AM*/
/*
revision: 1.1.0
Author: M. Cassim Farook
Date: 21-Nov-2002 10:16 PM
Notes: No offense buddhike...but i had to rewrite the code
works for ADT (any dam thing)
usage: x = format_number(123.999, 2)
*/
/*
revision: 1.2.0
Authors: Buddhike de Silva
Date: 22-Nov-2002 12:07 PM
Notes: Optimized for best performence.
usage: x = format_number(123.999, 2)
*/
/*
* Revision: 1.3
* Author: Mike Robb (JS-X.com)
* Date: May 26, 2003
* Notes: Changed to deal with negative numbers.
* Fixed length of final answer.
* Work-around for javascript internal math problem with rounding negative numbers.
*/
/*
* Revision 1.4
* Author: LeAnn Roberts
* Date: September, 2003
* Note: Modified the if logic: Math.pow()
*/
/*
* Revision 1.5
* Author: Robert Heggdal
* Date: February, 2004
* Note: Modified check for negative number by replacing parseInt with parseFloat so that negative numbers between zero and minus one are recognized as such.
*/
/*
* Revision 1.6
* Author: Naveen
* Date: February, 2004
* Note: Rewrote format_number to correct a logic problem.
*/
/*
* Revision 1.7
* Author: JS-X.com
* Date: February, 2004
* Note: Added wrapper around format_number as negative values were dropped from
* the logic.
*/
/*
* 2008/08/01
* Make it available for Webkit: Zoltan Herczeg, 2008, University of Szeged
*/
function format_number(p,d)
{
var r;
if(p<0){p=-p;r=format_number2(p,d);r="-"+r;}
else {r=format_number2(p,d);}
return r;
}
function format_number2(pnumber,decimals)
{
var strNumber = new String(pnumber);
var arrParts = strNumber.split('.');
var intWholePart = parseInt(arrParts[0],10);
var strResult = '';
if (isNaN(intWholePart))
intWholePart = '0';
if(arrParts.length > 1)
{
var decDecimalPart = new String(arrParts[1]);
var i = 0;
var intZeroCount = 0;
while ( i < String(arrParts[1]).length )
{
if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
{
intZeroCount += 1;
i += 1;
}
else
break;
}
decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1));
Math.round(decDecimalPart);
decDecimalPart = parseInt(decDecimalPart)/10;
decDecimalPart = Math.round(decDecimalPart);
//If the number was rounded up from 9 to 10, and it was for 1 'decimal'
//then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0.
if(decDecimalPart==Math.pow(10, parseInt(decimals)))
{
intWholePart+=1;
decDecimalPart="0";
}
var stringOfZeros = new String('');
i=0;
if( decDecimalPart > 0 )
{
while( i < intZeroCount)
{
stringOfZeros += '0';
i += 1;
}
}
decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart);
var dot = decDecimalPart.indexOf('.');
if(dot == -1)
{
decDecimalPart += '.';
dot = decDecimalPart.indexOf('.');
}
var l=parseInt(dot)+parseInt(decimals);
while(decDecimalPart.length <= l)
{
decDecimalPart += '0';
}
strResult = decDecimalPart;
}
else
{
var dot;
var decDecimalPart = new String(intWholePart);
decDecimalPart += '.';
dot = decDecimalPart.indexOf('.');
var l=parseInt(dot)+parseInt(decimals);
while(decDecimalPart.length <= l)
{
decDecimalPart += '0';
}
strResult = decDecimalPart;
}
return strResult;
}
var seed = 0xbbd1;
function rand()
{
var a = (seed * 5) & 0xFFFF;
// Swap bytes
a = ((a & 0xFF) << 8) + (a >> 8);
seed = (((seed + a) & 0xff)<<8) + (a & 0xff);
// Return low byte
return (a & 0xff);
}
for (var i=0; i < 104000; i++) {
// ~ 0.17 sec
format_number((rand() | (rand() << 8) || (rand() << 16)) / ((rand() & 0xF) + 3), (rand() & 0x7) + 1)
}