转换为货币格式 [英] Convert to currency format

查看:100
本文介绍了转换为货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a built in function of JavaScript to convert a string into a currency format?

例如

For example

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

更新

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

Please note that I want the function to return the currency to include the US comma to group digits.

推荐答案


我已决定完全重写2009年的示例。请检查区别如果对旧版本感兴趣。为了实现像以前的答案一样的功能,我已经提取了我正在处理的 Money 库的一部分。

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.

我也不记得为什么我上次重新创建了 toFixed ,因为该方法已经存在。

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

String 数字 javascript中的对象,就像上次一样,我正在创建新的 Money ,object。

Instead of messing with String and Number objects in javascript, like last time, I am creating new, Money, object.

(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 对象并将添加到Money 方法。

Now, I do need to modify Number and/or String objects slightly and add toMoney method.

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

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

最后,我们可以将 String 和/或 Number Money 并将其写为 String

So, finally, we can convert String and/or Number to Money and write it out as String again.

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

I think this solution is much better than the last one I wrote. For working example check jsFiddle.

这篇关于转换为货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆