Reputation: 448
I am looking for an efficient way to return a string formatt of a float in javascript that is formatted in the following way:
#,##0
But I want to have trailing "spaces" instead of zero's if there is no decimals in order to have the numbers aligned in the following fashion (basically so that the decimal points all line up). If it has trailing padding, I can simply align to the right to have them align correctly.
10.12
101.3
10
Thanks,
Upvotes: 3
Views: 3464
Reputation: 28830
Does add commas and set the decimal part correctly.
function formatnum(n) {
var a = ((Math.round(n*1000) / 1000) + '').split('.');
var one = a[0];
while (one.match(/\d{4}(,|$)/)) one = one.replace(/(\d)(\d{3})(,|$)/, "$1,$2$3");
var two = (a[1] ? a[1] : '');
one += (two ? '.':' ');
while (two.length < 3) two += ' ';
return one + two;
}
Version with negative numbers
function formatnum(n) {
var sig = (n < 0);
if (sig) n = -n;
var a = ((Math.round(n*1000) / 1000) + '').split('.');
var one = a[0];
while (one.match(/\d{4}(,|$)/)) one = one.replace(/(\d)(\d{3})(,|$)/, "$1,$2$3");
var two = (a[1] ? a[1] : '');
one += (two ? '.':' ');
while (two.length < 3) two += ' ';
return (sig ? '-':'') + one + two;
}
Upvotes: 0
Reputation: 101604
// decimal_pad(number[, length of padding[, padding character]])
// if padding parameter is null, zeros are used to pad
// if length parameter is null, no padding is applied.
function decimal_pad(dec,len,chr){
chr = chr || '0';
dec = dec.toString();
if (!len) return dec;
var p = dec.indexOf('.');
p = (p!==-1?(dec.length-p-1):-1);
for (var m = p; m < len; m++)
dec += chr;
return dec;
}
Should suite your needs nicely.
Where:
var tests = [1,2.3,4.56,7.890];
for (var t = 0; t < tests.length; t++){
$('#data').append(decimal_pad(tests[t],3,' ')+'\r\n');
}
Returns:
1____
2.3__
4.56_
7.891
(Underscores as spaces for sake of visibility)
Upvotes: 3
Reputation: 3573
var padTo = 2
var num = 101.3
var numAsString = num.toString()
var dotAt = numAsString.indexOf('.')
var spacesToAdd = numAsString.length - dotAt
for(i=0;i<spacesToAdd;i=i+1)
{
numAsString = numAsString + ' '
}
Little rusty with Javascript but I think you can get the idea from this.
Upvotes: 2
Reputation: 14550
There's probably an easier way to do this with regular expressions, but here's my quick implementation:
function number_format(num, precision)
{
if(precision == undefined) precision = 3;
var split = num.toFixed(precision).split('.');
var len = split[1].length;
var nonzero = false;
var arr = [];
for(var i=len-1; i>=0; i--)
{
var char = split[1][i];
if(char > 0) nonzero = true;
if(nonzero) arr.unshift(char);
else arr.unshift(' '); // alternately: ' '
}
return split[0]+'.'+arr.join('');
}
// usage
var num_string = number_format(10.25);
var num_string = number_format(10.25, 5);
Upvotes: 0
Reputation: 19349
I would start with formatting the string with the trailing zeros. Then you should be able to apply a Regex to check for a string of 0's at the end of the string, and replace them with spaces.
Upvotes: 0