javascript - 转换为货币格式

标签 javascript html

是否有内置的 JavaScript 函数可以将字符串转换为货币格式?

例如

var a = '1234';
a.convertToCurrency();   // return $1,234

更新

请注意,我希望函数返回货币以包含美国逗号来分组数字。

最佳答案

I have decided to completely rewrite example I did in 2009. Please check diff if interested in older version. In order to achieve functionality like previous answer, I have extracted a part of the Money library I am working on.

I also don't remember why I have recreated toFixed last time as that method was already present. This time it is not included.

我没有像上次那样在 javascript 中搞乱 StringNumber 对象,而是创建新的 Money 对象。

(function() {
  window.Money = (function() {

    Money.prototype.amount = 0.0;
    Money.prototype.fraction_count = 2;
    Money.prototype.fraction_separator = ",";
    Money.prototype.separate_thousands = true;
    Money.prototype.symbol = "€";
    Money.prototype.symbol_position = "front";
    Money.prototype.symbol_spacing = false;
    Money.prototype.thousands_separator = ".";

    function Money(amount, options) {
      var o;
      if (options == null) {
        options = {};
      }
      for (o in options) {
        this[o] = options[o];
      }
      amount = parseFloat(amount);
      if (!isNaN(amount)) {
        this.amount = amount;
      }
      this.format();
    }

    Money.prototype.format = function() {
      this.string_amount = this.amount.toFixed(this.fraction_count);
      if (this.separate_thousands) {
        this.string_amount = this.separateThousands();
      }
      return this.string = this.addSymbol();
    };

    Money.prototype.separateThousands = function() {
      var after_dot, before_dot, pattern, _ref;
      _ref = this.string_amount.split("."), before_dot = _ref[0], after_dot = _ref[1];
      pattern = /(-?\d+)(\d{3})/;
      while (pattern.test(before_dot)) {
        before_dot = before_dot.replace(pattern, "$1" + this.thousands_separator + "$2");
      }
      return [before_dot, after_dot].join(this.fraction_separator);
    };

    Money.prototype.addSymbol = function() {
      var string;
      string = [this.string_amount];
      string.splice((this.symbol_position === "front" ? 0 : 1), 0, this.symbol);
      return string.join(this.symbol_spacing ? " " : "");
    };

    return Money;

  })();

现在,我确实需要稍微修改 Number 和/或 String 对象并添加 toMoney 方法。

Number.prototype.toMoney = function(options) {
  return new Money(this, options);
};

String.prototype.toMoney = function(options) {
  return new Money(this, options);
};

因此,最后,我们可以将 String 和/或 Number 转换为 Money 并将其写为 String 再次。

x = "1234567890.0987654321".toMoney();
y = 1234567890.0987654321.toMoney({fraction_count: 5, symbol: "$", symbol_position: "back"});

console.log(x);
// Money {amount: 1234567890.0987654, string_amount: "1.234.567.890,10", string: "€1.234.567.890,10"}

console.log(x.string)
// €1.234.567.890,10 

console.log(y);
// Money {fraction_count: 5, symbol: "$", symbol_position: "back", amount: 1234567890.0987654, string_amount: "1.234.567.890,09877"…}

console.log(y.string)
// 1.234.567.890,09877$ 

我认为这个解决方案比我写的上一个好得多。对于工作示例,请检查 jsFiddle

关于javascript - 转换为货币格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1718878/

相关文章:

javascript - 将 RFC 822 日期转换为 javascript 的有效日期

带闭包的 JavaScript 垃圾回收

html - HTML5 中卡片/卡片容器的元素?

javascript - .prototype 在 JavaScript 中如何工作?

html - 如何使图片填充整个容器并在 Bootstrap 中保持适当的纵横比?

javascript - 将后代 img 元素的 src 属性插入父元素的 href 属性

javascript - 我可以将带有文件输入元素的 WebRTC 音频 blob 对象作为普通文件上传但不下载吗?

javascript - 为什么我的 angularJS 没有在 DOM 中正确更新?

javascript - Predix UI Seed 应用程序 - 从 url 中删除哈希

php - 单击该行时将所选类添加到表行