颤振问题:如何从字符串转换这种类型的双精度数?

flutter problem : How to convert this type of double from string?

我想将 sting 转换为 double , 这里我的双人间用逗号隔开, 那怎么办呢?

void main() {
  var myInt = int.parse('12345');
  assert(myInt is int);
  print(myInt); // 12345
  print(myInt.runtimeType);  // int
  
  
    var myDouble = double.parse('1,230.45');
  assert(myInt is double);
  print(myDouble); 
  print(myDouble.runtimeType); // double
}

您需要先删除 , 才能将其解析为双精度:

var myDouble = double.parse('1,230.45'.replaceAll(',', ''));

您需要在解析之前删除逗号。 要删除逗号,您可以像这样在 String 上使用 replaceAll

void main() {
  var k1 = '1,230.45';
  var k2 = k1.replaceAll(',','');
  var myDouble = double.parse(k2);
  print(myDouble); 
  print(myDouble.runtimeType);
}

package:intlNumberFormat也可以解析以逗号作为分组分隔符的数字:

import 'package:intl/intl.dart';

void main() {
  var numberFormat = NumberFormat('#,###.##');
  var myDouble = numberFormat.parse('1,230.45');
  print(myDouble); // Prints: 1230.45
  print(numberFormat.format(myDouble)); // Prints: 1,230.45
}