How to Double Parse A String and Round It to 2 Decimal Places - flutter

I want the String values that are added from
List<String> priceStringList
and converted into Doubles to
List<double> doubleList
to only have 2 decimal places.
for (int j = 0; j < priceStringList.length; j++) {
var doubleData = double.parse(priceStringList[j]);
doubleList.add(doubleData);
}
Sample data for doubleList after the for loop include:
[3.429, 3.44, 3.49, 3.41683, 3.35501, 3.02, 3.06, 3.17947,...
But I want the values to only include the first 2 digits after the decimal point. I tried using
var doubleData = Math.Round(double.parse(priceStringList[j]), 2);
but I get an error stating "Undefined name 'Math'".
EDIT I have already added import 'dart:math';

Try this:
double roundDouble(double value, int places){
double mod = pow(10.0, places);
return ((value * mod).round().toDouble() / mod);
}
var doubleData = roundDouble(double.parse(priceStringList[j]), 2);

To call Math you have to import it as Math, for example:
import 'dart:math' as Math;

Related

How to get interpolation values between two list values in flutter

I have two different list values i want get the corresponding one list between value to other list between values. Please look at the list below. Here i have 1st list value of 11 and 15 here i need to find the between values of 12, 13 and 15.
11 value =22
15 value =30
List<double> list1 =[11, 15];
List<double> list2 =[22, 30];
If I understand correct you need the interpolation function to generate values between 2 points:
List<double> interpolate(double start, double end, int count) {
if (count < 2) {
throw Exception("interpolate: illegal count!");
}
final array = List.generate(count + 1, (index) => 0.0);
for (int i = 0; i <= count; ++i) {
array[i] = start + i * (end - start) / count;
}
return array;
}
Usage:
void main() {
final input = [1.0, 15.0];
print(interpolate(input.first, input.last, 4));
}
Result:
[1.0, 4.5, 8.0, 11.5, 15.0]

flutter - return/show plus sign of int

I'm trying to build some training app for functions for school. The problem is:
Everytime the randomly picked number is lower than 0, my function shows +-, because
I have a fixed format for my function.
EXAMPLE
I tried to use the NumberFormat of the Intl-Package, but then I can't use the int-values correctly. Is there a way to show a plus sign for positive numbers, while they are still usable to work with them?
Code so far:
int randomNumberMinMax(int min, int max){
int randomminmax = min + Random().nextInt(max - min);
if(randomminmax==0){
randomminmax = min + Random().nextInt(max - min);
}
//generate random number within minimum and maximum value
return randomminmax;
}
int a = randomNumberMinMax(-5, 5);
int b = randomNumberMinMax(-10, 10);
int c = randomNumberMinMax(-10, 10);
String task = "f(x) = $a(x+$b)²+ $c";
You could only show the plus when the number is positive like this for example
String task = "f(x) = $a(x${b >= 0 ? "+" : ""}$b)²${c >= 0 ? "+" : ""} $c";

Flutter find the sum of digits of int value

I want to find the sum of the digits of the number entered in Flutter. I want to encode this algorithm.
for example
x=1992
result=1+9+9+2=21
how can i do this with flutter
You can do in this way.
import 'dart:io';
void main() {
print('Enter X');
int X = int.parse(stdin.readLineSync()!);
int result = 0;
for (int i = X; i > 0; i = (i / 10).floor()) {
result += (i % 10);
}
print('Sum of digits\n$result');
}
Output
Enter X
123456
Sum of digits
21
transform the number into an String using String stringValue = x.toString();
create an array from each char using List<String> result = stringValue.split('');
sum each number transforming back using int.parse(result)
void main(){
int x = 1992;
String stringValue = x.toString();
List<String> result = stringValue.split('');
int sum = 0;
for(int i = 0 ; i < result.length; i++) {
int value = int.parse(result[i]);
sum = sum + value;
}
print(sum);
}
Result: 21

Random double generation between a range in dart

Need to generate random doubles between a range
The nextint() function takes a param max where as the nextdouble() doesnt take any params.
Is there any other methods that return random doubles between a range in dart?
The nextDouble returns a value between 0 and 1 (not included). So, if you want a number in the range a (included) to b (not included), you can just do:
double doubleInRange(Random source, num start, num end) =>
source.nextDouble() * (end - start) + start;
print(doubleInRange(random, a, b));
No, there isn't, but it easy to recreate it since nextInt takes only a max value (exclusive).
nextDouble() * max
I doubt there is. If you want just double values you can convert the integer value to double
import 'dart:math';
main() {
var rng = new Random();
for (var i = 0; i < 10; i++) {
print(rng.nextInt(100).toDouble());
}
}
If you want the type of double values generated by nextDouble() such as '0.2502033576383784' i suggest you create a function to handle the range of values.
import 'dart:math';
main() {
var rng = new Random();
for (var i = 0; i < 10; i++) {
print(rng.nextDouble() + rng.nextInt(50));
}
}
I have one more simple solution to add to the list(only with Max range).
var random = Random();
int randomInt = random.nextInt(45);
double randTemp = random.nextDouble() * randomInt;

How to convert a binary number into a decimal fraction in dart?

Hi i have been wondering if there is a way in which to convert binary numbers into decimal fractions.
I know how to change base as an example through this code
String binary = "11110010";
//I'd like to change this line so it produces a decimal value
String denary = int.parse(binary, radix: 2).toRadixString(10);
If anyone still wondering how to convert decimal to binary and the inverse:
print(55.toRadixString(2)); // Outputs 110111
print(int.parse("110111", radix: 2)); Outputs 55
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
This is my c++ solution but you can implement any language