admin管理员组

文章数量:1389754

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,

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,

Share Improve this question asked Feb 17, 2011 at 0:37 elieli 4484 silver badges11 bronze badges 1
  • Sorry about that, missed the "instead of zero's" part of the question. – Jeff Swensen Commented Feb 17, 2011 at 1:11
Add a ment  | 

5 Answers 5

Reset to default 3
// 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)

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.

Does add mas 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;
  }

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.

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: '&nbsp;'
     }

     return split[0]+'.'+arr.join('');
}


// usage
var num_string = number_format(10.25);
var num_string = number_format(10.25, 5);

本文标签: