LoginSignup
2
0

More than 5 years have passed since last update.

Angular(Angular2)で数値の書式化(underscore.stringライブラリ使用)

Posted at

数値の書式化ができる文字列ライブラリunderscore.stringをangular-cliで使用するためのメモ。

:zero: 動作確認環境

以下の環境で動作確認済みです。

# package version
1 angular-cli 1.0.0-beta.24
2 typescript 2.0.3
3 angular 2.3.1
4 underscore.string 3.3.4
5 @types/underscore.string 0.0.30

:one: underscore.string のインストール

まずはunderscore.string本体のインストール。

npm install underscore.string --save

続いて、angular-cliの設定。underscore.string.min.jsscriptsの中に追加。

angular-cli.json
"scripts": [
  "../node_modules/underscore.string/dist/underscore.string.min.js"
]

:two: @types/underscore.string のインストール

typescriptで使うためにd.ts定義ファイルをインストールする。

npm install @types/underscore.string --save-dev

:three: 使う

sample.ts
import * as s from 'underscore.string'

console.debug(s.lpad('1', 2, '0'));
console.debug(s.numberFormat(1000, 2));
console.debug(s.numberFormat(123456789.123, 5, ".", ","));
コンソール出力
01
1,000.00
123,456,789.12300

underscore.string自体の使い方は以下が参考になる。

2
0
0

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
2
0