How to avoid negative values in Flutter - 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.

Use abs() which converts any value to positive or absolute. Just like this,
String totamt = tot.abs().toString();

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

Related

remove decimal in dart

I have a number: 466.62 as a double and can't change it to a string, how can I get rid of the decimal point and print 46662? I've managed to get 46662.0 by multiplying the value by 100, but I don't want to print the decimal point. Thanks
When you multiply 466.62 by 100, your end result will remain a double as well. This is why you are seeing 46662.0. So you need to convert it to Int value, so instead you should do it like this:
double vDouble = 466.62 *100
String vString = vDouble.toInt().toString();
This will give you 4662.
For a more generic case, use the String split method;
String str = '466.62';
//split string
var arr = str.split('.');
print(arr[0]); //will print 466
print(are[1]); //will print 62
After you have managed to get this 46662.0,
Use this :
46662.0.toStringAsFixed(0);
print(46662.0.toStringAsFixed(0)); - This gives 46662
print(46662.0.toStringAsFixed(0).runtimeType); - This gives as String.
Reference :
https://api.dart.dev/stable/2.8.4/dart-core/num/toStringAsFixed.html
You can do integer division by 1 and then turn it into a string
final String number = ((466.62 * 100) ~/ 1).toString();
Or just truncate
final String number = (466.62 * 100).truncate().toString();
More on the ~/ operator https://api.flutter.dev/flutter/dart-core/num/operator_truncate_divide.html
To remove decimals from use effective integer division
int x = (12345679.900 ~/ 1000)
~/ is effective integer operator and removes decimals .

Take decimal numbers only in dart/flutter

I have a number that I want to take only the decimal part and convert it to an integer with certain precision.
How can I do that in Dart Language or flutter ?
For example :
turn this 247.64646122587197 into this 6464
Drop the float number and take only 4 decimals and convert it to an integer.
Something like this should work:
((x % 1) * pow(10, 4)).floor()
you can also do that like this
void main() {
final double abc=247.64646122587197;
int y = int.tryParse(abc.toString().split('.')[1].substring(0,4));
print(y);
}
output :6464
You could try this:
return (247.64646122587197 * 10000).toInt() % 10000;
double value = 247.64646122587197;
double decimalValue = value - value.toInt();
print(decimalValue.toStringAsFixed(4))

Dart round a figure but as a double

I want to multiply a figure and then have it rounded to remove any decimal places but returned as a double, rather than an int since that is what I need.
I've tried this:
double number = 84.5;
double coke = (number * 2.3).round()) as double;
and this:
double coke = double.parse(number * 2.3).round());
But I can't get it to be a double, it just throws errors. The value would be 194.35 in this case and I want it to be 194.0.
double number = 84.5;
double answer = (number * 2.3).roundToDouble(); // 194.0
CopsOnRoad's answer is the most direct way, but in general the way to convert an int to a double is to use int.toDouble:
double number = 84.5;
double coke = (number * 2.3).round().toDouble();
Using as double doesn't work because as changes what type the object is treated as but doesn't change the underlying object.

Decimal Value Set as 0 in Swift

I am trying to get a random decimal from 0.75 to 1.25
let incomeCalc = Decimal((arc4random_uniform(50)+75)/100)
print("incomeCalc")
print(incomeCalc)
Why does this print 0?
arc4random_uniform return an integer type so you are doing integer math. You need to be doing floating point math.
let incomeCalc = Decimal(Double((arc4random_uniform(50)+75))/100)
By casting the value before you do the division, you get a Double result which is passed to your Decimal initializer.
Or you can do:
let incomeCalc = Decimal((arc4random_uniform(50)+75))/100
which creates the Decimal before the division is done.
You can also use the code below which gets a random number between 75 - 125 and then divides it by 100
let incomeCalc = Decimal((arc4random_uniform(50)+75)) / 100
print("incomeCalc")
print(incomeCalc)

Performing operations on a double returns 0

I have a method that receives a number in a NSString format.
I wish to convert this string to a double which I can use to calculate a temperature.
Here's my method.
NSString *stringTemp = text; // text is a NSString
NSLog(#"%#",stringTemp); // used for debugging
double tempDouble = [stringTemp doubleValue];
NSLog(#"%f",tempDouble); // used for debugging
Please note I put the NSLog commands here just to see if the number was correct. The latter NSLog returns a value of 82.000000 etc. (constantly changes as it's a temperature).
Next I wanted to use this double and convert it to a Celsius value. To do so, I did this:
double celsiusTemp = (5 / 9) * (tempDouble - 32);
Doing this: NSLog(#"%d", celsiusTemp); , or this: NSLog(#"%f", celsiusTemp); both give me a value of 0 in the console. Is there any reason why this would be happening? Have I made a stupid mistake somewhere?
Thank you for your help!
Try doing (5.0 / 9.0). If you only use an int to do math where you are expecting a double to be returned (like 0.55) everything after the decimal place will be lost because the cpu expects an int to be returned.
5 / 9 is the division of two integers, and as such uses integer division, which performs the division normally and then truncates the result. So the result of 5 / 9 is always the integer 0.
Try:
double celsiusTemp = (5.0 / 9) * (tempDouble - 32);
If you evaulate (5/9) as an integer, then it is just 0.