格式化字符串数作为货币

时间:2016-03-09 19:19:31

标签: javascript regex string-formatting money-format

我想要一个掩码函数来格式化数字(作为字符串),就像许多掩码插件一样。

期望的行为:

    . . .
    var logoRange = _xlSheet.Range[
        _xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn+1]];
    PlacePicture(_logo, logoRange);
}

// From Jürgen Tschandl
private void PlacePicture(Image picture, Range destination)
{
    Worksheet ws = destination.Worksheet;
    Clipboard.SetImage(picture);
    ws.Paste(destination, false);
    Pictures p = ws.Pictures(System.Reflection.Missing.Value) as Pictures;
    Picture pic = p.Item(p.Count) as Picture;
    ScalePicture(pic, (double)destination.Width, (double)destination.Height);
}

// Also from Jürgen Tschandl
private void ScalePicture(Picture pic, double width, double height)
{
    double fX = width / pic.Width;
    double fY = height / pic.Height;
    double oldH = pic.Height;
    if (fX < fY)
    {
        pic.Width *= fX;
        if (pic.Height == oldH) // no change if aspect ratio is locked
            pic.Height *= fX;
        pic.Top += (height - pic.Height) / 2;
    }
    else
    {
        pic.Width *= fY;
        if (pic.Height == oldH) // no change if aspect ratio is locked
            pic.Height *= fY;
        pic.Left += (width - pic.Width) / 2;
    }
}

如果格式化程序仅使用正则表达式函数实现这一点会更好。

[编辑]

在stackoverflow中搜索了很多问题,但大多数答案包括在字符串末尾添加两个小数。那不是我的预期。

预期:

format('1') // '0,01'
format('12') // '0,12'
format('123') // '1,23'
format('12345678') // '123.456,78'

未预料到:

format('1') // '0,01'
format('123') // '1,23'

1 个答案:

答案 0 :(得分:1)

这是一个两步解决方案:将数字分为3位数字和1-2位数字

^(?:(\d+)(\d{2})|(\d{1,2}))$

请参阅regex demo

因此,大于1的数字将在第1组中具有整数部分,在第2组中包含小数部分,而第3组将包含小于0的所有小数。

然后,我们检查我们得到的数字,只用

将千位分隔符加到整数部分
.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.")

参见JS演示:

&#13;
&#13;
function format(s) {
  var re = /^(?:(\d+)(\d{2})|(\d{1,2}))$/; 
  if ((m = s.match(re)) !== null) {   
      if (m[3]) {                     // We have 1 or 2 digit number
          return m[3].length == 1 ? 
              "0,0" + m[3] : "0," + m[3];
      } else {                       // We have a longer number
          return m[1].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.") + "," + m[2];
      }
    
    }
}

document.body.innerHTML = format('1') + "<br/>"; // '0,01'
document.body.innerHTML += format('12') + "<br/>"; // '0,12'
document.body.innerHTML += format('123') + "<br/>"; // '1,23'
document.body.innerHTML += format('12345678') + "<br/>"; // '123.456,78'
&#13;
&#13;
&#13;

实际上,您可以添加千位和小数分隔符作为函数参数:

&#13;
&#13;
function format(str, thd, dec) {
  var re = /^(?:(\d+)(\d{2})|(\d{1,2}))$/; 
  if ((m = str.match(re)) !== null) {   
    if (m[3]) {                     // We have 1 or 2 digit number
      return m[3].length == 1 ? 
         "0" + dec + "0" + m[3] : "0" + dec + m[3];
    } else {                       // We have a longer number
         return m[1].replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1" + thd) + dec + m[2];
    }
  }
}

var thd = "."; // Thousand separator
var dec = ","; // Decimal separator
document.body.innerHTML = format('1', thd, dec) + "<br/>"; // '0,01'
document.body.innerHTML += format('12', thd, dec) + "<br/>"; // '0,12'
document.body.innerHTML += format('123', thd, dec) + "<br/>"; // '1,23'
document.body.innerHTML += format('12345678', thd, dec) + "<br/>"; // '123.456,78'
&#13;
&#13;
&#13;