0

I have scoured the internet and I haven't found a solution that really works for me, yet.

var tv = Length * Type;

if (tv < 0) 
    {
    cForm.voltage.value = "-" + Math.abs(tv) + " V";
    }
else...

Some of the calculations with these two numbers come out to about the 15th decimal for some reason. I would like to limit the decimal amount that is returned, and NOT allow the number to round up or down. On a calculator it only comes out to about the third decimal, but Math.abs() brings it too far out.

.toFixed() Doesn't work for me because if the number only has 2 decimals it will add additional zeros at the end. I only want to display up to the fourth if it is calculated.

CrunchyToast
  • 105
  • 11
  • Please provide example input values that demonstrate the problem. – Dai Sep 14 '15 at 21:36
  • 1
    `"-" + Math.round(Math.abs(tv) * 100) / 100 + " V"` – GOTO 0 Sep 14 '15 at 21:36
  • Somehow the people asking for how to always show n decimals managed to find solutions for your problem: http://stackoverflow.com/q/2221167/218196, http://stackoverflow.com/q/1726630/218196 – Felix Kling Sep 14 '15 at 21:44
  • 1
    I already stumbled upon those 2 links and they didn't work for me. @GOTO0 this solution worked. Thank you. – CrunchyToast Sep 14 '15 at 21:53
  • That solution is exactly what people posted in the questions... Don't you see it? `Math.round(Math.abs(tv) * 100) / 100 ` as suggested by GOTO 0 is the same as `Math.round(price*Math.pow(10,2))/Math.pow(10,2);` used in [this question](http://stackoverflow.com/q/1726630/218196). How come this didn't work if it is the same? [The other question says](http://stackoverflow.com/q/2221167/218196): *"in JavaScript, the typical way to round a number to N decimal places is something like: ... However this approach will round to a maximum of N decimal places..."* which is exactly what you want. – Felix Kling Sep 14 '15 at 23:32
  • 1
    Thanks for the talk and no help whatsoever @FelixKling. Have a good evening. – CrunchyToast Sep 14 '15 at 23:45

3 Answers3

2

Just expanding on @goto-0 s comment, with the correct # of decimal places.

var tv = Length * Type;

if (tv < 0) 
    {
        cForm.voltage.value = "-" + (Math.round(Math.abs(tv) * 10000) / 10000) + " V";
    }
else...
TbWill4321
  • 8,626
  • 3
  • 27
  • 25
1

Here's the implementation as a function that truncates the extra decimal places. If you want to round the output you could just use Number.toPrecision().

function toFixedDecimals(num, maxDecimals) {
  var multiplier = Math.pow(10, maxDecimals);
  return Math.floor(num * multiplier) / multiplier
}

console.log(toFixedDecimals(0.123456789, 4));
console.log(toFixedDecimals(100, 4));
console.log(toFixedDecimals(100.12, 4));
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Dave
  • 10,748
  • 3
  • 43
  • 54
0

I'm sure its not the most efficient approach but it is pretty brainless -

  1. grab your result
  2. split it into an array based on the decimal point
  3. then trim the decimal part to two digits (or however many you would like).
  4. concat the pieces back together

Sorry for the long variable names - just trying to make it clear what was happening : )

    // your starting number - can be whatever you'd like
    var number = 145.3928523;
    // convert number to string
    var number_in_string_form = String(number);
    // split the number in an array based on the decimal point
    var result = number_in_string_form.split(".");
    // this is just to show you what values you end up where in the array
    var digit = result[0];
    var decimal = result[1];
    // trim the decimal lenght to whatever you would like
    // starting at the index 0 , take the next 2 characters
    decimal = decimal.substr(0, 2);
    // concat the digit with the decimal - dont forget the decimal point!
    var finished_value = Number(digit + "." + decimal); 

In this case the finished_value would = 145.39

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
domdambrogia
  • 2,054
  • 24
  • 31