Vishali
Vishali

Reputation: 1397

How to avoid negative values in Flutter

double totTax = double.parse(nontax_amt) - double.parse(tax);
        String tottax = totTax.toString();
        double igst = double.parse(igstper) / 100 * double.parse(tottax);
        double cAmt = double.parse(widget.itemDetailsList[position].cgstRate) /
                      100 *double.parse(tottax);
          double total =totTax +igst +cAmt;
          String totamt = tot.toString();

I am calculating some double values called totTax,igst,cAmt and it's giving values with negative sign. I want the positive answer instead of negative values, meaning whatever value I try to calculate it should not give answer with negative sign.

Note: Because of negative sign the value of total is becoming wrong.

Upvotes: 1

Views: 2520

Answers (2)

Jahidul Islam
Jahidul Islam

Reputation: 12575

try with abs() method

double totTax = (double.parse(nontax_amt) - double.parse(tax)).abs();
double igst = (double.parse(igstper) / 100 * double.parse(tottax)).abs();
double cAmt = (double.parse(widget.itemDetailsList[position].cgstRate) /
                      100 *double.parse(tottax)).abs();

Upvotes: 2

Yash V. Monpara
Yash V. Monpara

Reputation: 151

Use abs() which converts any value to positive or absolute. Just like this,

String totamt = tot.abs().toString();

Upvotes: 2

Related Questions