4umi.com/web/javascript/numberformat

# Numbers formatted

## Math Javascript

The Arabic system of number notation, together with Simon Stevin's invention of the decimal dot, make a powerful team. Yet to display numbers which are out of the human proportion, requires a little script sometimes.

```function commanumber(N) {
N = '' + N;
if( N.length>3 ) {
var mod = N.length%3;
var output = ( mod>0 ? (N.substring(0,mod)) : '' );
for( var i=0; i<Math.floor( N.length/3 ); i++ ) {
if( mod*i===0 ) { output += N.substring( mod+3*i, mod+3*i+3 ); }
else { output += ',' + N.substring( mod+3*i, mod+3*i+3 ); }
}
return output;
}
return N;
}

function significantnumber( N, significance ) {
if(typeof significance==='undefined') { significance = 2; }
N = Math.round( N * Math.pow( 10, significance ) ) / Math.pow( 10, significance );
return N; // may need padding
}

function sciencenumber(N) {
var b = 0;
while (N>10) {
N /= 10;
b ++;
}
return N + '*10^' + b;
}

function nosciencenumber(N) {
var a = 0, b = 0; N = N.split( /\*\s*?10\s*?\^/ );
if( N.length===2 ) {
a = N[0];
b = N[1];
}else{
a = window.prompt( 'Enter the number that is the decimal:', '1' );
b = window.prompt( 'Enter the power that is given to 10:', '2' );
}
return a * Math.pow( 10, b );
}
```