iMagicBae

Rounding function in GS2

Jan 9th, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function round(temp.number) {
  2.     // Divide the number into whole and decimals.
  3.   temp.tokens = temp.number.tokenize(".");
  4.     // This means they've sent a whole number. Return the whole number
  5.   if (temp.tokens.size() == 1) { return temp.number; }
  6.  
  7.     // Get the last decimal on the number.
  8.   temp.last = temp.number.charat(temp.number.length() - 1);
  9.     // Determine whether or not rounding is needed.
  10.   temp.increment = (temp.last >= 5);
  11.  
  12.     // If it rounds up to another decimal...
  13.   if (temp.tokens[1].length() > 1) {
  14.     temp.last = temp.tokens[1].charat(temp.tokens[1].length() - 2) + temp.increment;
  15.     temp.rounded = temp.tokens[1].substring(0, temp.tokens[1].length() - 2) @ temp.last;
  16.     return temp.tokens[0] @ "." @ temp.rounded;
  17.   } else {
  18.     return int(temp.tokens[0] + temp.increment);
  19.   }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment