Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the operation % in Dart return a strange value% work in Dart

Tags:

dart

The var c return 3 but 10/7=1.4285, the rest is 0.4285, operator % has a bug?

void main() {
  var a = 10;
  var b = 7;
  var c;
  c = a % b;
  print(c);
}
like image 625
Alexandre Avatar asked Oct 12 '25 16:10

Alexandre


1 Answers

The '%' operator returns the remainder left after dividing two numbers. It does not return the decimal part. For example:

10 / 7

    1
  ______
7 ) 10
   - 7
  ______
     3

So it returns 3 which is what remains after dividing 10 by 7 without any decimals. 10 / 7 = 1 3/7

What you want to do can be accomplished like this:

var floatNumber = 12.5523;
var x = floatNumber - floatNumber.truncate();

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!