Posted By

sekihin on 05/06/09


Tagged

javascript number


Versions (?)

Round Down


 / Published in: JavaScript
 

  1. //数値を切り捨てる
  2. function roundDown(value,num){
  3. var a,b,c,i;
  4. a = String(value);
  5. b = a.indexOf('.');
  6. c = a.length;
  7. if (num==0)
  8. {
  9. if (b!=-1)
  10. a = a.substring(0,b);
  11. }
  12. else
  13. {
  14. if (b==-1)
  15. {
  16. a = a + ".";
  17. for (i=1;i<=num;i++)
  18. a = a + "0";
  19. }
  20. else
  21. {
  22. a = a.substring(0,b+num+1);
  23. for (i=c;i<=b+num;i++)
  24. a = a + "0";
  25. }
  26. }
  27. return a;
  28. }
  29.  
  30. roundDown(123.45,1); // 123.4
  31. roundDown(123.45,0); // 123

Report this snippet  

You need to login to post a comment.