Reputation: 329
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
Upvotes: 7
Views: 16969
Reputation: 605
To remove decimals from use effective integer division
int x = (12345679.900 ~/ 1000)
~/
is effective integer operator and removes decimals .
Upvotes: 1
Reputation: 121
You can do integer division by 1 and then turn it into a string
final String number = ((466.62 * 100) ~/ 1).toString();
Or just truncate
final String number = (466.62 * 100).truncate().toString();
More on the ~/ operator https://api.flutter.dev/flutter/dart-core/num/operator_truncate_divide.html
Upvotes: 3
Reputation: 655
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
Upvotes: 9
Reputation: 1784
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
Upvotes: 19