Home > OS >  Flutter how to avoid negative values
Flutter how to avoid negative values

Time:09-22

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 its 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. please help me to achieve this.

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

Thanks in Advance

CodePudding user response:

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

CodePudding user response:

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

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