Skip to content

Instantly share code, notes, and snippets.

@erluxman
Created June 29, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erluxman/9611894aef886b8918e486757422e229 to your computer and use it in GitHub Desktop.
Save erluxman/9611894aef886b8918e486757422e229 to your computer and use it in GitHub Desktop.
Num dounding to 1, 1.5, 2.0 et
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