grinyam

function float

May 5th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function float(answer, x, y) {
  2.       var count = yLength = xLength = 0;
  3.  
  4.       if (x < 1 && x > -1) {
  5.         if (x > 0) {
  6.           while (x < 1) {
  7.             x = x * 10;
  8.             count++;
  9.           }
  10.           xLength = count;
  11.         }
  12.  
  13.         if (x < 0) {
  14.           while (x > -1) {
  15.             x = x * 10;
  16.             count++;
  17.           }
  18.         }
  19.         xLength = count;
  20.       }
  21.  
  22.       if (y < 1 && y > -1) {
  23.         count = 0;
  24.  
  25.         if (y > 0) {
  26.           while (y < 1) {
  27.             y = y * 10;
  28.             count++;
  29.           }
  30.           yLength = count;
  31.         }
  32.  
  33.         if (y < 0) {
  34.           while (y > -1) {
  35.             y = y * 10;
  36.             count++;
  37.           }
  38.           yLength = count;
  39.         }
  40.       }
  41.  
  42.       if (yLength == 0 && xLength == 0) {
  43.         return answer;
  44.       }
  45.  
  46.       if (yLength >= xLength) {
  47.         return parseFloat(answer).toFixed(yLength);
  48.       }
  49.  
  50.       if (xLength >= yLength) {
  51.         return parseFloat(answer).toFixed(xLength);
  52.       }
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment