LoginSignup
0
0

More than 5 years have passed since last update.

accounting-jsって何?

Last updated at Posted at 2017-10-19

accounting.js

通貨、通貨フォーマットを提供してくれるライブラリ。

使い方は読みこむだけ。

<script src="path/to/accounting.js"></script>

<script type="text/javascript">
    // Library ready to use:
    accounting.formatMoney(5318008);
</script>

できること

  • カスタム出力形式
  • 数値の解析/非フォーマット
  • 簡単なローカライゼーション
  • スプレッドシート形式の列書式設定(行の記号と小数点以下の桁数の設定)

ライブラリメソッドの一部

formatMoney() - 任意の数値を通貨にフォーマットする

  //デフォルトの使用法:
 accounting.formatMoney(12345678);  // $ 12,345,678.00

 //ヨーロッパの書式設定(カスタムシンボルとセパレータ)では、optionsオブジェクトを第2パラメータとして使用することもできます。
 accounting.formatMoney(4999.99, "", 2, "", "");  // 4.999,99ユーロ

 //負の値は正式にフォーマットできます:
 accounting.formatMoney(-500000, "£", 0);  //£500,000

 //単純な `format`文字列はシンボルの位置を制御します(%v = value、%s = symbol):
 accounting.formatMoney(5318008, {symbol: "GBP", 形式: "%v%s"});  // 5,318,008.00 GBP 

formatNumber() - カスタム精度とローカライズで数値をフォーマットする

1000の位で「,」を入れたいなら、これを使えばOK

accounting.formatNumber(5318008);  // 5,318,008
accounting.formatNumber(9876543.21, 3, "");  // 9 876 543.210 

具体的な使い方

    import accounting from 'accounting-js'

//標準的な使用法とパラメータ(文字列を返す)
accounting.formatMoney(number,[symbol = "$"],[precision = 2],[thousand = ","],[decimal = "."],[format = "%s%v"])

//第2のパラメータはオブジェクトにできる
accounting.formatMoney(number, [options])

// options.currencyに一致する利用可能なオプション
var options = {
    symbol : "$",
    decimal : ".",
    thousand: ",",
    precision : 2,
    format: "%s%v"
};

// 使用例:
accounting.formatMoney(12345678); // $12,345,678.00
accounting.formatMoney(4999.99, "", 2, ".", ","); // €4.999,99
accounting.formatMoney(-500000, "£ ", 0); // £ -500,000

// optionsオブジェクトを使用した使用例
accounting.formatMoney(5318008, {
    symbol: "GBP",
    precision: 0,
    thousand: "·",
    format: {
        pos : "%s %v",
        neg : "%s (%v)",
        zero: "%s  --"
    }
});

// 値の配列を再帰的にフォーマット
accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]

簡単に遊んでみた感じをgithubに。
acctountingで実験

=======

//便利な書き方1
accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]


//便利な書き方2
accounting.formatMoney(number, [options])

var options = {
    symbol : "$",
    decimal : ".",
    thousand: ",",
    precision : 2,
    format: "%s%v"
};

//便利な書き方3
accounting.formatNumber(9876543); // 9,876,543

`accounting.formatNumber だけで乗り切れそうな感じはした。

参考
accounting.jsのドキュメント

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0