0

I'm sending the number/string 0.001 to a the function below:

SignificantFigures = 4;
function LimitNumberOfDigits(num) {
    var tempStr = "";
    if (isNaN(num))
        return "\xD8";
    else{
        if (parseFloat(num) === 0 || (num.toString().indexOf('.') === -1 && parseInt(num) < 9999) || num.toString().length <= 4) {
            return num;
        }
        tempStr = parseFloat(num).toPrecision(SignificantFigures);
        if (tempStr.indexOf("e") > -1) {
            var startE = tempStr.indexOf("e");
            var endE = 0;
            for (var i = startE +2 ; i < tempStr.length; i++ ) { // + to ignore e and sign (+ or - )
                if(parseInt(tempStr[i], 10) > 0) {
                    endE = i;
                }else {
                    break;
                }
            }
            if (startE + 2 === endE) {
                var pow = tempStr[endE];
            } else {
                var pow = tempStr.substring(startE +2 ,endE);
            }
            return tempStr.substring(0,startE) + "*10<sup>"+ pow +"</sup>";
        }else {
            return parseFloat(num).toPrecision(SignificantFigures);
        }
    }
}

When im sending 0.2 or even 0.11 im getting like 0.2000 and 0.1100. The issue here is the toPrecision acts like ToFixed. Ideas?

EDIT

What i want? simple as that, if a numbers needs to be changed e.g 0.012312312041 it should be 0.0123 , numbers like 0.12 or 28 should stay the same.

Vera Gavriel
  • 381
  • 2
  • 5
  • 14
  • 1
    The behavior of `toPrecision` and `toFixed` will be the same for those numbers. E.g. 0.2 to 2 significant figures = 0.20. That's because leading zeros *are not significant*. Try it with 0.004 to 3 sig figs vs 0.004 `toFixed(3)` - you'll see a difference then. – RobH Jul 24 '13 at 07:51
  • so what do i have to do in order to keep numbers that i dont need to convert? – Vera Gavriel Jul 24 '13 at 07:57
  • also numbers like 28 turns into 28.00 – Vera Gavriel Jul 24 '13 at 07:59
  • k, so what can i add to this function to do what i need? – Vera Gavriel Jul 24 '13 at 08:01
  • Simple. Convert the number to a string first. Then check the length of the string. If it's too long, then set the precision of the number. Voila! Short numbers stay short, long numbers get shortened. – enhzflep Jul 24 '13 at 08:09
  • This answer may be what you want: http://stackoverflow.com/a/202476/1402923 – RobH Jul 24 '13 at 08:17
  • @enhzflep look the function now, is it better? added condition to 2nd if statment about length – Vera Gavriel Jul 24 '13 at 08:37
  • @VeraGavriel - I didn't try the function before it's modification. I think you forgot to test it before posting this? I've just tried your code with the following numbers, 10.00005, 10.00006, 0.012312312041, 0.12 and 28. Unfortunately, the results are: 10.00, 10.00, 0.01231, 0.12 and 28. So, the code works in places and fails in others. :( For the two values of 10.00, you need to account for the number of digits found before the decimal place. I.e toPrecision(4) won't work. You'll need toPrecision(6) for those two. – enhzflep Jul 24 '13 at 11:51
  • @enhzflep i need them to be 10 and not 10.0000 thats the problem..now they are 10.00 .. irrelevent zeros after period – Vera Gavriel Jul 25 '13 at 06:22
  • @VeraGavriel - If I could make a suggestion, please update the question with your refined requirements. You said that `0.012312312041` should display as `0.0123` - by extension, (using the same rules) `10.00005` and `10.00006` should have 4 digits after the decimal point. If you don't want to display 10.0000, then after you've reduced it to having 4 places following the decimal point, add a test to see if all digits following the decimal place are 0. If they are, then simply discard the decimal point and all digits that follow it. I suggest you add some (more) numbers and the expected result. – enhzflep Jul 25 '13 at 06:59
  • @enhzflep already added it in my first edit. look what i want to have. – Vera Gavriel Jul 25 '13 at 08:57
  • @VeraGavriel - Yeah, I saw that already - you can see from my second comment that I already used those 3 pairs of input/expected output. I merely used 10.00005 and 10.00006 to check for rounding behaviour. You'll need to provide a more comprehensive set of inputs and their expected outputs to get better answers! – enhzflep Jul 25 '13 at 17:54

0 Answers0