11

Is there anyway to round up a double value?

I want result always rounded up.

int offSet = (totalRecords / 10).round();
0

4 Answers 4

30

It's ceil:

Returns the least integer no smaller than this.

int offSet = (totalRecords / 10).ceil();
-3

try with

num.parse((totalRecords / 10).toStringAsFixed(3))

if you want 3 decimal

2
  • 1
    They want an int, not String.
    – bereal
    Commented Feb 18, 2020 at 14:19
  • 1
    There is no sense in needless string conversions for something which can be handled perfectly using numerical types and simple function calls.
    – nanofarad
    Commented Feb 19, 2020 at 4:15
-3

Now you have something like you want. I choose sup to 5 to round up, you can change if you want

num offSet = (totalRecords / 10);
var eval = offSet.toStringAsFixed(1).split('.');
var res =
    int.parse(eval[1]) > 5 ? int.parse(eval[0]) + 1 : int.parse(eval[0]);
print(res);
-3

Here I'm rounding it to the next double or to next 0.5; Sample: If its 6.6 then rount to 7.0. If its 6.2, then round to 6.5. See code bellow:

String arredonde(String n) {
final List x = n.split('.'); //break in to a list
if (x.length > 1) { //if its 0, then its already a rounded number or integer
  int fstNmbr = int.parse(x[0]);
  final int lstNmbrs = int.parse(x[1]);
  if (lstNmbrs > 5) {
    fstNmbr = fstNmbr + 1;
    final String finalNumber = fstNmbr.toStringAsFixed(1);
    return finalNumber;
  } else {
    if (lstNmbrs != 0) {
      final double finalNumber = fstNmbr + 0.5;
      return finalNumber.toStringAsFixed(1);
    } else {
      return n;
    }
  }
} else {
  return n;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.