Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove decimal in dart

Tags:

flutter

dart

I have a number: 466.62 as a double and can't change it to a string, how can I get rid of the decimal point and print 46662? I've managed to get 46662.0 by multiplying the value by 100, but I don't want to print the decimal point. Thanks

like image 256
James Piner Avatar asked Jul 10 '26 04:07

James Piner


2 Answers

When you multiply 466.62 by 100, your end result will remain a double as well. This is why you are seeing 46662.0. So you need to convert it to Int value, so instead you should do it like this:

double vDouble = 466.62 *100
String vString = vDouble.toInt().toString();

This will give you 4662.

For a more generic case, use the String split method;

String str = '466.62';

//split string
var arr = str.split('.');

print(arr[0]); //will print 466
print(are[1]); //will print 62
like image 150
Mohammad Assad Arshad Avatar answered Jul 11 '26 18:07

Mohammad Assad Arshad


After you have managed to get this 46662.0,

Use this :

46662.0.toStringAsFixed(0);

print(46662.0.toStringAsFixed(0)); - This gives 46662

print(46662.0.toStringAsFixed(0).runtimeType); - This gives as String.

Reference : https://api.dart.dev/stable/2.8.4/dart-core/num/toStringAsFixed.html

like image 27
Tanmay Pandharipande Avatar answered Jul 11 '26 17:07

Tanmay Pandharipande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!