使用两位小数格式化数字,用空格而不是 0 填充

发布于 2024-10-18 02:05:21 字数 224 浏览 5 评论 0原文

我正在寻找一种有效的方法来返回 JavaScript 中浮点数的字符串格式,其格式如下:

#,##0

但如果没有小数,我希望使用尾随“空格”而不是零,以便使数字对齐以下方式(基本上使小数点全部对齐)。如果它有尾部填充,我可以简单地向右对齐以使它们正确对齐。

 10.12
101.3
 10

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初与友歌 2024-10-25 02:05:21
// 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;
}

应该很好地满足您的需求。

其中:

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');
}

返回:(

1____
2.3__
4.56_
7.891

为了可见性,下划线作为空格)

// 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)

扭转时空 2024-10-25 02:05:21
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 + ' '
}

Javascript 有点生疏,但我想你可以从中得到启发。

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.

才能让你更想念 2024-10-25 02:05:21

是否添加逗号并正确设置小数部分。

  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;
  }

带负数的版本

  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;
  }

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;
  }
别闹i 2024-10-25 02:05:21

我首先使用尾随零格式化字符串。然后您应该能够应用正则表达式来检查字符串末尾是否有 0 字符串,并将它们替换为空格。

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.

风吹雪碎 2024-10-25 02:05:21

可能有一种更简单的方法可以使用正则表达式来做到这一点,但这是我的快速实现:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文