Round off to nearest hundred in dart - flutter

I came to a part in my flutter app where I need to round up to the nearest hundred and thought that there was probably some way to do it but I guess not. So I searched the net for examples or any answers and I've yet to find any since all examples appear to be to the nearest hundred. I just want to do this and round UP. Maybe there's some simple solution that I'm overlooking. I have tried double.roundToDouble() but it does not work for me. If anyone could help me with this issue I would greatly appreciate it.
If my number is 199.03, I want the result rounded to 200.00.
199.08->200.00
99.30->100.00
14.99->15.00
499.09->500.00
What I tried
.roundToDouble (this does not work as 199.08.roundToDouble returns as 199.0)

double val = 199.03;
int roundedToNearestHundred = ((val + 50) ~/ 100) * 100).toInt();
void main() {
List<double> v = [
199.03,
1250.00,
1249.99,
1250.01,
49.99,
50.00,
50.01,
];
v.forEach((val) => print((((val + 50) ~/ 100) * 100).toInt()));
}
This will give in result:
200
1300
1200
1300
0
100
100

Here is an example I wrote. You can try it by pasting it dartpad.dev
List<double> examples = [
199.08,
99.3,
14.99,
499.09,
];
for (int i = 0; i < examples.length; i++) {
double num = examples[i];
double roundedNumber = num.ceilToDouble();
print("$num -> $roundedNumber");
}
You can use ceil method to achieve this.
RESULT:
199.08 -> 200
99.3 -> 100
14.99 -> 15
499.09 -> 500

Related

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

Any way to make text count display in, K and M

If my text reaches 1000 i want it to turn into 1K, and 1200 to be 1.2K. Same with millions.
1000K would turn into 1M, 1100 to turn into 1.1M. Is there anyway to do this. (This is unity c#).
Just create a class that will convert it. I would create a class NumberConverter or something like that and method inside:
public string GetConvertedNumber(float numb)
{
string convertedNumber;
if(numb < 1000)
{
convertedNumber = numb.ToString();
}
else if(numb >= 1000 && numb < 1000000)
{
convertedNumber = $"{(numb / 1000) Round it to 1 place after dot.ToString()}K"
}
}

Flutter float number rounding

I want to achieve the following in my code, and can't seem to find a proper solution:
I need code that will always show 6 digits of a number, no matter being int greater than 999999 or floating point smaller than 0.
100000 -> 100000
1000000 -> 100000
10000.0 -> 10000
100.01111 -> 100.011
0.000001 -> 0
0.000011 -> 0.00001
With some help in the comments, I got a solution that works for me. If someone has more elegant way of doing this please do share it.
int desiredPrecision = 6;
int numberOfDigitsOnTheLeft = val.toInt().toString().length;
String sixDigitString = val.toStringAsFixed(desiredPrecision-numberOfDigitsOnTheLeft);
as an option
void main() {
print(_normalizeNum(10000.0));
print(_normalizeNum(100000));
print(_normalizeNum(1000000));
print(_normalizeNum(10000.0));
print(_normalizeNum(100.01111));
print(_normalizeNum(0.000001));
print(_normalizeNum(0.000011));
}
String _normalizeNum(num d) {
d = d.clamp(double.negativeInfinity, 999999);
d = num.parse(d.toStringAsFixed(6).substring(0, 7));
if (d == d.toInt()) {
d = d.toInt();
}
return d.toString();
}

Learning swift, Issues with incrementing variables

I'm back again with what is likely a simple issue, however its got me stumped.
I've written very small, very basic piece of code in an xcode playground.
My code simply iterates over a function 10 times, printing the output each time.
var start = 0
var x = 0
var answer = 2 * x
func spin() {
print(answer)
}
while start < 10 {
spin()
x++
start++
}
Now for my issue, It seems my code properly increments the 'start' variable.... running and printing 10 times. However it prints out a list of 0's. For some reason the 'x' variable isn't incrementing.
I've consulted the few ebooks I have for swift, aswell as the documentation, and as far as i can see my code should work.
Any ideas?
P.s. As per the documentation I have also tried ++x, to no avail.
edit
Updated, working code thanks to answers below:
var start = 0
var x = 0
var answer = 2 * x
func spin() {
print("The variable is", x, "and doubled it is", answer)
}
while start <= 10 {
spin()
x++
start++
answer = 2 * x
}
You have just assigned 2 * x to answer at the beginning of the program, when x == 0, and the value of answer remains its initial value through out the program. That's how Value Types work in Swift as well as in almost any other languages
If you wish to always have answer to be 2 times of x, you should write like this
var start = 0
var x = 0
var answer = 2 * x
func spin() {
print(answer)
}
while start < 10 {
spin()
x++
start++
answer = 2 * x
}
And thanks to Leo Dabus's answer, you may also define a Computed Property to caculate the value of 2 * x each time you try to get the value of answer. In this way, answer becomes readonly and you cannot assign other values to it. And each time you try to get the value of answer, it performs the 2 * x calculation.
var start = 0
var x = 0
var answer: Int {
return 2 * x
}
func spin() {
print(answer)
}
while start < 10 {
spin()
x++
start++
}
What you need is a read only computed property. Try like this:
var answer: Int { return 2 * x }

Scaling Up a Number

How do I scale a number up to the nearest ten, hundred, thousand, etc...
Ex.
num = 11 round up to 20
num = 15 round up to 20
num = 115 round up to 200
num = 4334 round up to 5000
I guess this formula might work? Unless you have more examples to show.
power = floor(log10(n))
result = (floor(n/(10^power)) + 1) * 10^power
import math
exp = math.log10(num)
exp = math.floor(exp)
out = math.ceil(num/10**exp)
out = out * 10**exp
Convert the number to a decimal (i.e. 11 goes to 1.1, 115 goes to 1.15), then take the ceiling of the number, then multiply it back. Example:
public static int roundByScale(int toRound) {
int scale = (int)Math.pow(10.0, Math.floor(Math.log10(toRound)));
double dec = toRound / scale;
int roundDec = (int)Math.ceil(dec);
return roundDec * scale;
}
In this case, if you input 15, it will be divided by 10 to become 1.5, then rounded up to 2, then the method will return 2 * 10 which is 20.
public static int ceilingHighestPlaceValue(int toCeil)
{
int placeValue = Math.Pow(10,toCeil.ToString().Length()-1);
double temp = toCeil / placeValue;
return= ceil(temp) * placeValue;
}