22
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Flutter]数字を三桁ごとにカンマ区切りで表示する

Last updated at Posted at 2020-02-04

はじめに

金額の単位を表示したい時などに使うやつです。

今回はNumberFormatを利用しました
NumberFormat class - intl library - Dart API

import

import 'package:intl/intl.dart';

3桁ごとにカンマ区切り

final formatter = NumberFormat("#,###.0");
var result = formatter.format(50101214);
print(result); // 50,101,214

3桁区切り小数点1桁まで

金額の単位を表示したい時など

final formatter = NumberFormat("#,###.0");
var value = (50101214 / 10000);
var result = formatter.format(value);
print(result+'万円'); //5,010.1万円

3桁区切り小数点2桁まで

final formatter = NumberFormat("#,###.0#");
var value = (50101214 / 10000);
var result = formatter.format(value);
print(result+'万円'); //5,010.12万円

他にも書き方等ある場合はコメントいただけると助かります。

2020/02/10追記 Stringにも対応

こちらの記事を参考にさせていただきました。
https://stackoverflow.com/questions/31931257/dart-how-to-add-commas-to-a-string-number

Function mathFunc = (Match match) => '${match[1]},';
RegExp reg = RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))');
String result = '20000円'.replaceAllMapped(reg, mathFunc);
print('$result');//20,000円
22
14
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
22
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?