Home > Back-end >  flutter - return/show plus sign of int
flutter - return/show plus sign of int

Time:10-11

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";

CodePudding user response:

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";
  • Related