Link to home
Start Free TrialLog in
Avatar of sunshine737
sunshine737

asked on

Truncating without Rounding a number using javascript or xsl?

Truncating without Rounding a number using javascript or xsl?

i shall pass the "number of digits to truncate".

examples:

number   digitsTruncate       Output

123.456        (3)              123.456
123.456        (2)              123.45
123.456789   (7)              123.4567890
123               (2)              123.00
123              (3)              123.000
123.4            (2)              123.40
123.4            (4)              123.4000



Thank you,

Regards
vh
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Use toFixed() method of Number objects.
Like this:

<script>
alert((123.456).toFixed(3)+
"\n"+(123.456).toFixed(2)+
"\n"+(123.456789).toFixed(7)+
"\n"+(123).toFixed(2)+
"\n"+(123).toFixed(3)+
"\n"+(123.4).toFixed(2)+
"\n"+(123.4).toFixed(4));
</script>

ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Zvonko,

I think toFixed() is rounding the number.

i.e.

    var number = 123.456;
    alert(number.toFixed(2));

yields 123.46 and not 123.45

Batalf
toFixed() rounds (as said)

<script>
  function truncDigits(inputNumber,digits){
    fact= Math.pow(10,digits)    
    return Math.floor(inputNumber*fact)/fact
  }
   
  number = 123.456789
  alert(truncDigits(number,4))
  alert(truncDigits(number,3))
  alert(truncDigits(number,0))
</script>

That was also my first idea Gwyn, but then I tested with nums < 0.

Here my prototype:

<script>

Number.prototype.fixTo = fixToPlaces;

alert((123.456).fixTo(3)+
"\n"+(123.456).fixTo(2)+
"\n"+(123.456789).fixTo(7)+
"\n"+(123).fixTo(2)+
"\n"+(123).fixTo(3)+
"\n"+(123.4).fixTo(2)+
"\n"+(123.4).fixTo(4));



function fixToPlaces(p){
  for(var s="",i=0;i<p;i++) s+="0";
  var x = (this+'.').split('.')
  x = x[0]+((p>0)?"."+(x[1]+s).substr(0,p):"");
  return x;
}

</script>


Slightly better readable is this one:

<script>
Number.prototype.fixTo = fixToPlaces;

alert((123.456).fixTo(3)+
"\n"+(123.456).fixTo(2)+
"\n"+(123.456789).fixTo(7)+
"\n"+(123).fixTo(2)+
"\n"+(123).fixTo(3)+
"\n"+(123.4).fixTo(0)+
"\n"+(123.4).fixTo(2)+
"\n"+(123.4).fixTo(4));



function fixToPlaces(p){
  if(p<1) return parseInt(this);
  for(var s="",i=0;i<p;i++) s+="0";
  var x = (this+'.').split('.')
  x = x[0]+"."+(x[1]+s).substr(0,p);
  return x;
}

 

</script>

I hate to flooding this thread, but I had to remove the last line :)

function fixToPlaces(p){
  if(p<1) return parseInt(this);
  for(var s="",i=0;i<p;i++) s+="0";
  var x = (this+'.').split('.')
  return x[0]+"."+(x[1]+s).substr(0,p);
}


My function works for -ve's as well in the sense that it cuts off the extra digits. which I believe is what is required here.
I used here your power factor for protopyping  :)

<script>
Number.prototype.toX = Number.prototype.toFixed;
Number.prototype.toFixed = fixNoRound;
function fixNoRound(p){
   var f = Math.pow(10,p);
   var n = Math.floor(this*f)/f;
   return n.toX(p);
}

alert((123.456).toFixed(3)+
"\n"+(123.456).toFixed(2)+
"\n"+(123.456789).toFixed(7)+
"\n"+(123).toFixed(0)+
"\n"+(0.01).toFixed(5)+
"\n"+(123).toFixed(3)+
"\n"+(123.4).toFixed(2)+
"\n"+(123.4).toFixed(4));
</script>

Sorry, I misspelled the word prototyping.
It shoild read: "I used here your power factor for prototyping."
Avatar of Anarchon
Anarchon

Ah, the just of type interconversion...this version leaves you with a string. That has the sole virtue of conserving trailing zeroes, if you intend to maintain the significance of such.

---
<html>

<head>
   <script>
      function truncateToDecimalPlace(amt, dec) {
      // Return string, containing amount >amt< truncated to >dec< decimal places.
         if (dec < 0) return 'bad call to truncateToDecimalPlace(): dec = ' + dec;
         var str = '' + amt;  // Force conversion to string type, if numeric.
         if (-1 == str.indexOf('.')) str += '.';
         str += '0000000000';  // should handle reasonable cases
         return str.slice(0, str.indexOf('.') + dec + (dec == 0 ? 0 : 1));
      }
alert('fmtToDecimalPlace(123.15678, 4) is >' + truncateToDecimalPlace(123.15678, 4) + '<');
alert('fmtToDecimalPlace(123.15678, 2) is > ' + truncateToDecimalPlace(123.15678, 2) + '<');
alert('fmtToDecimalPlace(123.9, 0) is > ' + truncateToDecimalPlace(123.9, 0) + '<');
   </script>
</head>

<body>
</body>

</html>
Why not using parseInt() ?

var value = 123.45;
value = parseInt(value);
alert(value);
ah, never mind.
The accepted solution is incorrect.
Here is the correction to the accepted solution:

    function truncateNumber(inputNumber, digits){        
        var number = Math.floor(inputNumber);
        var fdecimal = (inputNumber - number) + '';
        if(fdecimal.length > (digits + 2))
            fdecimal = fdecimal.substr(0, (digits + 2));
        var returnValue = (number/1 + fdecimal/1);
        return returnValue.toFixed(digits);            
    }

function truncateNumber(inputNumber, digits){        
        var number = Math.floor(inputNumber);
        var fdecimal = (inputNumber - number) + '';
        if(fdecimal.length > (digits + 2)) 
            fdecimal = fdecimal.substr(0, (digits + 2));
        var returnValue = (number/1 + fdecimal/1);        
        return returnValue.toFixed(digits);            
    }

Open in new window