1

I need a js function that show the original count of decimals in a number. For example:

value          display
2.31             2
1.0              1
2.3500           4

The problem is that i dont know how get the count of decimals. I have that code: value=2.3500; return CountofDecimals(value); // must be display 4:

Anything help??? Thanks :P

Esailija
  • 138,174
  • 23
  • 272
  • 326
Reinier
  • 13
  • 3

4 Answers4

4

That's not possible. There's no difference between the number 3.5 and 3.50 in JavaScript, or indeed in any other common programming language.

If you actually mean they're strings (value = '2.3500' rather than value = 2.3500) then you can use indexOf:

var decimalPlaces = value.length - value.indexOf('.') - 1;
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • @user1549375: Then it just isn't possible. `2.3500` is stored internally as `2.35` and nothing will give you the right value. – Ry- Jul 24 '12 at 17:25
  • My function receives the number from c# and other functions used this number to make other calculations. So i can't change the type of this number. I will add another field but string. Thanks to all. :D – Reinier Jul 24 '12 at 17:31
  • @user1549375: You can pass a string and use `+value` to turn it into a number for the other functions, by the way. – Ry- Jul 24 '12 at 17:38
1

Caveat: I hate this answer, I don't really advocate it

Don't store it as a number, store it as a string. This can result in "stringly typed" code quickly so it is inadvisable. It is a workaround since JavaScript uses a float as the number type.

Alternatively store it as an Object and parse out the format via a function call:

{ value = "1.2345", decimal = 4} 

and use that to create the correct number format. If I had the requirement this is probably the hack I'd use. Or, I would have my server return the formatted string as you can pull that off easily server side.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Daniel B. Chapman
  • 4,647
  • 32
  • 42
  • My function receives the number from c# and other functions used this number to make other calculations. So i can't change the type of this number. I will add another field but string. Thanks to all. :D – Reinier Jul 24 '12 at 17:31
1

If it would be possible take these numbers as strings, it definitely is possible..And quite simple actually.

function countDecimals(string){
   var delimiters = [",","."];
   for(var i = 0; i<delimiters.length; i++){

        if(string.indexOf(delimiters[i])==-1) continue;
        else{
        return string.substring(string.indexOf(delimiters[i])+1).length;
    }
 }
}
simekadam
  • 7,334
  • 11
  • 56
  • 79
  • 2
    You should never use `for...in` with an array. – jbabey Jul 24 '12 at 17:26
  • This is a decent answer, why the downvotes? It may not be the best answer possible, but it solves the problem! – starbeamrainbowlabs Jul 24 '12 at 17:44
  • sorry for my vote. i'm novice with that. how i can vote +? :( – Reinier Jul 24 '12 at 17:53
  • @user1549375: You can't yet, you need 15 reputation (don't worry, it's not hard to get). The checkmark is for the right answer and can only be put on one answer. – Ry- Jul 24 '12 at 17:54
  • mmmm and the numbers betwen arrows? :D – Reinier Jul 24 '12 at 17:57
  • @Reinier: That's the vote count, other people can vote too. So check the answer that was right, and if you want, you can come back and vote all the ones that helped you up once you get 15 reputation. – Ry- Jul 24 '12 at 18:50
0

You could use this function:

function decimalplaces(number)
{
    numberastring = number.toString(10);
    decimalpoint = numberastring.indexOf(".");

    if(decimalpoint == -1)
    {
       return 0;
    }
    else
    {
        return numberastring.length - decimalpoint - 1;
    }
}
starbeamrainbowlabs
  • 5,692
  • 8
  • 42
  • 73