Question regarding Flutter.
I wish to display the result of a sum as either a int or a double. (I believe this is a 'num' right?)
I am sorry if this seems vague or super simple or whatever. I am a code noob.
At the moment I have:
TextEditingController weight = new TextEditingController();
TextEditingController distance1 = new TextEditingController();
TextEditingController distance2 = new TextEditingController();
String result ='0';
(Then I have some text input fields with the corresponding text controllers attached) A button performs and displays the math:
onPressed: (){
setState((){
num sum1 = num.parse(weight.text) * num.parse(distance1.text) ~/ num.parse(distance2.text);
result = sum1.toString();
});
},
I have some text which displays the result but this is always an int.
I have tried to convert 'result' to a num also using num.parse along with other various ideas but nothing seems to work. I have no idea what is suppose to be done.
Many thanks for your help.
CodePudding user response:
In your case, u can use double because of the result can have a fraction
onPressed: (){
setState((){
double sum1 = double.parse(weight.text) * double.parse(distance1.text) / double.parse(distance2.text);
result = sum1.toString();
});
},
if you get only specific value of fraction use
sum1.toStringAsFixed(3) // here 3 is your faction value what you want