blue492
blue492

Reputation: 670

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

Upvotes: 1

Views: 564

Answers (3)

Ashok
Ashok

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

Balasubramani Sundaram
Balasubramani Sundaram

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

Akif
Akif

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

Related Questions