Reputation: 670
How to convert 100.00 or 100.0 to 100 and if the number is 100.23 it should keep it as the same 100.23.
In dart, I tested these functions floor() and round() they return 100 even if the number is 100.23.
How to solve it?
Thanks
Upvotes: 1
Views: 564
Reputation: 3611
double input1 = 100.00; //100
double input2 = 100.0; //100
double input3 = 100.23; //100.23
RegExp regex = RegExp(r"([.]*0)(?!.*\d)");
String str = input1.toString().replaceAll(RegExp(r"([.]*0)(?!.*\d)"), "");
Upvotes: 2
Reputation: 1290
By using the intl package of flutter to format the number. please refer below
import 'package:intl/intl.dart' as intl;
@override
void initState() {
var valueFormat = intl.NumberFormat.decimalPattern();
print(valueFormat.format(100.234));
print(valueFormat.format(100.00));
}
OutPut
I/flutter ( 5364): 100.234
I/flutter ( 5364): 100
Upvotes: 4
Reputation: 7650
Until you get a better answer, you can do something like the following steps:
double value = 100.32739273;
String formattedValue = value.toStringAsFixed(2);
print(formattedValue);
if (formattedValue.endsWith(".00")) {
formattedValue = formattedValue.replaceAll(".00", "");
}
print(formattedValue);
Upvotes: 1