3

I want to convert sting to double , here my double is saperated by comma, So how to do it?

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
}

enter image description here

3 Answers 3

3

You need to remove , first to parse it as double:

var myDouble = double.parse('1,230.45'.replaceAll(',', ''));
Sign up to request clarification or add additional context in comments.

Comments

0

You need to remove the comma before parsing it. to remove comma you can use replaceAll on Stirng like this

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

Comments

0

package:intl's NumberFormat also can parse numbers with commas as grouping separators:

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.