如何在Flutter中将货币格式化为百万?

huangapple go评论73阅读模式
英文:

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 &#39;${number/1000000} m&#39;;
}

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 = &lt;String&gt;[];
while (number &gt; 0) {
result.add(_numToWord(number % base));
number = (number / base).floor();
}
if (result.length &gt; 4) return &#39;&#39;;
for (var i = 0; i &lt; result.length; i++) {
if (result[i] != &#39;&#39; &amp;&amp; i != 0) result[i] += &#39; ${scale[i]} و &#39;;
}
result = result.reversed.toList();
var words = result.join(&#39;&#39;);
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 = &#39;&#39;;
while (unit &gt; 0) {
if ((number / unit).floor() * unit != 0) {
if (numberToWord.containsKey(number)) {
result += numberToWord[number]!;
break;
} else {
result += &#39;${numberToWord[(number / unit).floor() * unit]} و &#39;;
number %= unit;
}
}
unit = (unit / 10).floor();
}
return result;
}
const scale = [&#39;&#39;, &#39;هزار&#39;, &#39;میلیون&#39;, &#39;میلیارد&#39;];
const base = 1000;
const endsWithAnd = &#39; و &#39;;
const numberToWord = {
0: &#39;&#39;,
1: &#39;یک&#39;,
2: &#39;دو&#39;,
3: &#39;سه&#39;,
4: &#39;چهار&#39;,
5: &#39;پنج&#39;,
6: &#39;شش&#39;,
7: &#39;هفت&#39;,
8: &#39;هشت&#39;,
9: &#39;نه&#39;,
10: &#39;ده&#39;,
11: &#39;یازده&#39;,
12: &#39;دوازده&#39;,
13: &#39;سیزده&#39;,
14: &#39;چهارده&#39;,
15: &#39;پانزده&#39;,
16: &#39;شانزده&#39;,
17: &#39;هفده&#39;,
18: &#39;هجده&#39;,
19: &#39;نوزده&#39;,
20: &#39;بیست&#39;,
30: &#39;سی&#39;,
40: &#39;چهل&#39;,
50: &#39;پنجاه&#39;,
60: &#39;شصت&#39;,
70: &#39;هفتاد&#39;,
80: &#39;هشتاد&#39;,
90: &#39;نود&#39;,
100: &#39;صد&#39;,
200: &#39;دویست&#39;,
300: &#39;سیصد&#39;,
400: &#39;چهارصد&#39;,
500: &#39;پانصد&#39;,
600: &#39;ششصد&#39;,
700: &#39;هفتصد&#39;,
800: &#39;هشتصد&#39;,
900: &#39;نهصد&#39;,
};
String trim(String string) =&gt; string.replaceAll(whiteSpaceRegExp, &#39;&#39;);
RegExp whiteSpaceRegExp = new RegExp(r&#39;^\s+|\s+$&#39;);

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(&#39;1515&#39;); //0.002M
test(&#39;1215&#39;); //0.001M
test(&#39;151555&#39;); // 0.152M
test(&#39;1511445&#39;); // 0.151M
test(&#39;15114455&#39;); // 15.1M
String test(String str) {
var result = NumberFormat.compact(locale: &#39;en&#39;).format(int.parse(str));
if (result.contains(&#39;K&#39;) &amp;&amp; result.length &gt; 3) {
result = result.substring(0, result.length - 1);
var prefix = (result.split(&#39;.&#39;).last.length) + 1;
var temp = (double.parse(result) * .001).toStringAsFixed(prefix);
result = double.parse(temp).toString() + &#39;M&#39;;
}
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 &gt;= 1000 &amp;&amp; n &lt; 1000000) {
return num.substring(0, len - 3) + &#39;.&#39; + num.substring(len - 3, 1 + (len - 3)) + &#39;k&#39;;
} else if (n &gt;= 1000000 &amp;&amp; n &lt; 1000000000) {
return num.substring(0, len - 6) + &#39;.&#39; + num.substring(len - 6, 1 + (len - 6)) + &#39;m&#39;;
} else if (n &gt; 1000000000) {
return num.substring(0, len - 9) + &#39;.&#39; + num.substring(len - 9, 1 + (len - 9)) + &#39;b&#39;;
} 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(&#39;${num/1000000} m&#39;);
}

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 &quot;${(number / 1000000).toStringAsFixed(2)} M&quot;;
}
double generatedNumber = 50000;
print(generate(generatedNumber));

huangapple
  • 本文由 发表于 2023年1月9日 17:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定