Created
June 29, 2020 04:35
-
-
Save erluxman/9611894aef886b8918e486757422e229 to your computer and use it in GitHub Desktop.
Num dounding to 1, 1.5, 2.0 et
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main(){ | |
print(round(2.4)); | |
print(round(2.1)); | |
print(round(2.6)); | |
print(round(2.8)); | |
} | |
double round(double num){ | |
double decimal = double.parse((num - num.toInt()).toStringAsFixed(1)); | |
double newDecimal = 0.0; | |
if(decimal>0 && decimal <= 0.5){ | |
newDecimal = 0.5; | |
} | |
else{ | |
newDecimal = 1.0; | |
} | |
return num.toInt() + newDecimal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment