Search code examples
flutterdartdouble

How to convert 100.00 to 100 and 100.23 to 100.23 dart


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


Solution

  • 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);