Decimal to Octal Function

Home

This function converts a decimal number into an octal number.

//**************************************
//     
// Name: Javascript function oct()
// Description:Duplicates the oct functi
//     on in vb.
// By: Ben White
//
// Inputs:number
//
// Returns:Returns a string representing
//     the octal value of a number.
//
// Assumes:If number is not already a wh
//     ole number, it is rounded to the nearest
//     whole number before being evaluated.
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.3003/lngWId.2/qx/vb/scripts/ShowCode
//     .htm//for details.//**************************************
//     
 
function Oct(int) {
  var r = '';
  var h = new Array('0','1','2','3','4','5','6','7');
  var n = Math.abs(int);
  var sign = (int == n);
  n = Math.round(n);
  for(x=0;x<32;x++) {
    if (Math.pow(8,x+1) > n) {break}
  }
  for (y=x;y>=0;y--) {
    z = Math.pow(8,y);
    r+=h[Math.floor(n/z)];
    n = n % z;
  }
  return (r.length == 0)?'0':((sign)?r:'-'+r);
}