Parsing / Converting Dart Value From String To Int(Number), To Double And Vice Versa

Simple ways of converting types in dart.

String to Number: parseInt

We want to convert a string to a number(int). We use the parseInt method.

1
2
3
4
5
void main(){
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
}

Number to String: toString

1
2
3
4
void main(){
var myString = (12345).toString;
print(myString); // '12345'
}

String to double: parseDouble

1
2
3
4
void main(){
var myDouble = double.parse('12345');
print(myDouble); // 12345.0
}

Number to double: toDouble

1
2
3
4
void main(){
var myDouble = (12345).toDouble();
print(myDouble); // 12345.0
}

Number to Radix String: toRadixString(radix)

1
2
3
4
void main(){
var myRadixString = (12345).toRadixString(2);
print(myRadixString); // 11000000111001
}

For more dart types conversions, check this link.. https://api.dartlang.org/stable/2.3.1/dart-core/num-class.html#instance-methods

Related Posts

0 Comments

12345

    00