英文:
How to format money as millions in flutter?
问题
我已经使用数字格式化器将500,000格式化为500k,像这样:
NumberFormat.compact().format(int.parse(player.price!))
但我想将数字转换为半百万,像这样0.5m。
是否可能?
英文:
I have formatted 500000 to 500k using Number formatter like this
NumberFormat.compact().format(int.parse(player.price!))
but i want to convert the number to half million like this 0.5m
is it possible ?
答案1
得分: 1
这个函数会返回你想要的结果:
void main(){
print(convertNumber(500000));
}
String convertNumber(int number) {
return '${number/1000000} m';
}
输出结果:
0.5 m
正如你所知,如果你想要其他格式,你需要自定义它。
我认为你是伊朗人。
下面的代码对你来说可能很有用,可以将任何数字转换为文本:
void main(){
print(convertNumberToWord(500000));
}
String convertNumberToWord(int number) {
var result = <String>[];
while (number > 0) {
result.add(_numToWord(number % base));
number = (number / base).floor();
}
if (result.length > 4) return '';
for (var i = 0; i < result.length; i++) {
if (result[i] != '' && i != 0) result[i] += ' ${scale[i]} و ';
}
result = result.reversed.toList();
var words = result.join('');
if (words.endsWith(endsWithAnd)) {
words = words.substring(0, (words.length - 3));
}
words = trim(words);
return words;
}
String _numToWord(int number) {
var unit = 100;
var result = '';
while (unit > 0) {
if ((number / unit).floor() * unit != 0) {
if (numberToWord.containsKey(number)) {
result += numberToWord[number]!;
break;
} else {
result += '${numberToWord[(number / unit).floor() * unit]} و ';
number %= unit;
}
}
unit = (unit / 10).floor();
}
return result;
}
const scale = ['', 'هزار', 'میلیون', 'میلیارد'];
const base = 1000;
const endsWithAnd = ' و ';
const numberToWord = {
0: '',
1: 'یک',
2: 'دو',
3: 'سه',
4: 'چهار',
5: 'پنج',
6: 'شش',
7: 'هفت',
8: 'هشت',
9: 'نه',
10: 'ده',
11: 'یازده',
12: 'دوازده',
13: 'سیزده',
14: 'چهارده',
15: 'پانزده',
16: 'شانزده',
17: 'هفده',
18: 'هجده',
19: 'نوزده',
20: 'بیست',
30: 'سی',
40: 'چهل',
50: 'پنجاه',
60: 'شصت',
70: 'هفتاد',
80: 'هشتاد',
90: 'نود',
100: 'صد',
200: 'دویست',
300: 'سیصد',
400: 'چهارصد',
500: 'پانصد',
600: 'ششصد',
700: 'هفتصد',
800: 'هشتصد',
900: 'نهصد',
};
String trim(String string) => string.replaceAll(whiteSpaceRegExp, '');
RegExp whiteSpaceRegExp = new RegExp(r'^\s+|\s+$');
输出结果:
پانصد هزار
祝编码愉快...
英文:
this function below returns you the result you want:
void main(){
print(convertNumber(500000));
}
String convertNumber(int number) {
return '${number/1000000} m';
}
output:
0.5 m
as you know if you want to have another formats you have to customize it.
I think you are iranian.
these codes at below can be useful for you to change any numbers to text:
void main(){
print(convertNumberToWord(500000));
}
String convertNumberToWord(int number) {
var result = <String>[];
while (number > 0) {
result.add(_numToWord(number % base));
number = (number / base).floor();
}
if (result.length > 4) return '';
for (var i = 0; i < result.length; i++) {
if (result[i] != '' && i != 0) result[i] += ' ${scale[i]} و ';
}
result = result.reversed.toList();
var words = result.join('');
if (words.endsWith(endsWithAnd)) {
words = words.substring(0, (words.length - 3));
}
words = trim(words);
return words;
}
String _numToWord(int number) {
var unit = 100;
var result = '';
while (unit > 0) {
if ((number / unit).floor() * unit != 0) {
if (numberToWord.containsKey(number)) {
result += numberToWord[number]!;
break;
} else {
result += '${numberToWord[(number / unit).floor() * unit]} و ';
number %= unit;
}
}
unit = (unit / 10).floor();
}
return result;
}
const scale = ['', 'هزار', 'میلیون', 'میلیارد'];
const base = 1000;
const endsWithAnd = ' و ';
const numberToWord = {
0: '',
1: 'یک',
2: 'دو',
3: 'سه',
4: 'چهار',
5: 'پنج',
6: 'شش',
7: 'هفت',
8: 'هشت',
9: 'نه',
10: 'ده',
11: 'یازده',
12: 'دوازده',
13: 'سیزده',
14: 'چهارده',
15: 'پانزده',
16: 'شانزده',
17: 'هفده',
18: 'هجده',
19: 'نوزده',
20: 'بیست',
30: 'سی',
40: 'چهل',
50: 'پنجاه',
60: 'شصت',
70: 'هفتاد',
80: 'هشتاد',
90: 'نود',
100: 'صد',
200: 'دویست',
300: 'سیصد',
400: 'چهارصد',
500: 'پانصد',
600: 'ششصد',
700: 'هفتصد',
800: 'هشتصد',
900: 'نهصد',
};
String trim(String string) => string.replaceAll(whiteSpaceRegExp, '');
RegExp whiteSpaceRegExp = new RegExp(r'^\s+|\s+$');
output:
پانصد هزار
happy coding...
答案2
得分: 1
以下是翻译好的内容:
- Flutter提供的官方方法很难做到这一点,所以我写了这个
test('1515'); //0.002M
test('1215'); //0.001M
test('151555'); // 0.152M
test('1511445'); // 0.151M
test('15114455'); // 15.1M
String test(String str) {
var result = NumberFormat.compact(locale: 'en').format(int.parse(str));
if (result.contains('K') && result.length > 3) {
result = result.substring(0, result.length - 1);
var prefix = (result.split('.').last.length) + 1;
var temp = (double.parse(result) * .001).toStringAsFixed(prefix);
result = double.parse(temp).toString() + 'M';
}
return result;
}
请注意,代码部分没有翻译。
英文:
- The official method provided by Flutter is difficult to do this, so I wrote this
test('1515'); //0.002M
test('1215'); //0.001M
test('151555'); // 0.152M
test('1511445'); // 0.151M
test('15114455'); // 15.1M
String test(String str) {
var result = NumberFormat.compact(locale: 'en').format(int.parse(str));
if (result.contains('K') && result.length > 3) {
result = result.substring(0, result.length - 1);
var prefix = (result.split('.').last.length) + 1;
var temp = (double.parse(result) * .001).toStringAsFixed(prefix);
result = double.parse(temp).toString() + 'M';
}
return result;
}
答案3
得分: 0
String numberFormat(int n) {
String num = n.toString();
int len = num.length;
if (n >= 1000 && n < 1000000) {
return num.substring(0, len - 3) + '.' + num.substring(len - 3, 1 + (len - 3)) + 'k';
} else if (n >= 1000000 && n < 1000000000) {
return num.substring(0, len - 6) + '.' + num.substring(len - 6, 1 + (len - 6)) + 'm';
} else if (n > 1000000000) {
return num.substring(0, len - 9) + '.' + num.substring(len - 9, 1 + (len - 9)) + 'b';
} else {
return num.toString();
}
}
英文:
String numberFormat(int n) {
String num = n.toString();
int len = num.length;
if (n >= 1000 && n < 1000000) {
return num.substring(0, len - 3) + '.' + num.substring(len - 3, 1 + (len - 3)) + 'k';
} else if (n >= 1000000 && n < 1000000000) {
return num.substring(0, len - 6) + '.' + num.substring(len - 6, 1 + (len - 6)) + 'm';
} else if (n > 1000000000) {
return num.substring(0, len - 9) + '.' + num.substring(len - 9, 1 + (len - 9)) + 'b';
} else {
return num.toString();
}
}
答案4
得分: 0
只需除以 1000000
void main() {
double num = 500000;
print('${num/1000000} m');
}
输出为
0.5 m
英文:
Just divide by 1000000
void main() {
double num = 500000;
print('${num/1000000} m');
}
The output is
0.5 m
答案5
得分: 0
String generate(double number) {
return "${(number / 1000000).toStringAsFixed(2)} M";
}
double generatedNumber = 50000;
print(generate(generatedNumber));
英文:
String generate(double number) {
return "${(number / 1000000).toStringAsFixed(2)} M";
}
double generatedNumber = 50000;
print(generate(generatedNumber));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论