[Flutter Dart] 세 자리마다 쉼표로 구분하여 숫자를 표시합니다.

1625 단어 FlutterDarttech

개시하다


금액 단위를 표시하기 위해 사용하는 물건입니다.
이번엔 넘버포머를 이용했어요.
NumberFormat class - intl library - Dart API

import


import 'package:intl/intl.dart';

쉼표로 세 자리를 나누다


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

세 자리 구분자 최대 한 자리 소수점


금액 단위를 표시하고 싶을 때
final formatter = NumberFormat("#,###.0");
var value = (50101214 / 10000);
var result = formatter.format(value);
print(result+'万円'); //5,010.1万円

세 자리 구분자 최대 두 자리


final formatter = NumberFormat("#,###.0#");
var value = (50101214 / 10000);
var result = formatter.format(value);
print(result+'万円'); //5,010.12万円
다른 문법이 있다면 메시지를 남겨주세요.

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円

좋은 웹페이지 즐겨찾기