-1

I am using javascript number.toFixed() function.

My problem is I have to explicitly mention number of fractional length like below:

if number is 1e11 then I have to specifiy it as

number.toFixed(2) 

or if number is 1e1 then :

 number.toFixed(1)     

or if its 1e0 then

number.toFixed(0)

I want to do it dynamically based on whether fractions present or not. and irrespective of fractional length.

I dont want to pass parameter of fixed fractional length. js should dynamically understand fractional length of it. Is there similar provision in JS? or is there any similar function in java script where I need not to explicitly pass parameter of fixed length?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62
  • You've stated a problem, but not asked a specific question – Jamiec Aug 03 '15 at 09:18
  • Isn't that the exact point of the toFixed-function? What are you trying to acomplish? – mariusnn Aug 03 '15 at 09:18
  • actually I dont want to pass parameter of fixed fractional length. js should dynamically understand fractional length of it – Pramod S. Nikam Aug 03 '15 at 09:21
  • 1
    "Understand" as in "guess the programmers intent"? Yeah, it doesn't work like that. – Jamiec Aug 03 '15 at 09:22
  • Edited question to add more clearification – Pramod S. Nikam Aug 03 '15 at 09:26
  • @Orion youve not really clarified anything. When you say "dynamically understand" what *rules* do you want to put in place? What is the desired output for a given input? – Jamiec Aug 03 '15 at 09:29
  • Have you tried just calling `number.toString()`? If the intention is to just get a string representation of your number, that might do what you want. – Anthony Grist Aug 03 '15 at 09:31
  • @Orion are you sure that the **1e0.11** will accept as number.? just try this – Himesh Aadeshara Aug 03 '15 at 09:35
  • @Jamiec - I simply want to avoid passing parameter to number.toFIxed function and then I want out put as fixed value of given number and that should also with exact precision. I don't know how to achieve it. – Pramod S. Nikam Aug 03 '15 at 09:36
  • @HimeshAadeshara - yeah it will surely get cast as number as it is scientific notation. – Pramod S. Nikam Aug 03 '15 at 09:38
  • @Orion i think it should be like 123e5; // 12300000 z = 123e-5 //0.00123; i have never seen like 1e0.11 it shows **Unexpected number** just try out using console – Himesh Aadeshara Aug 03 '15 at 09:45
  • 1
    @Orion [MDN's documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Floating-point_literals) disagrees with you: "The exponent part is an "e" or "E" followed by **an integer**" 0.11 is not an integer, so 1e0.11 isn't valid JavaScript. – Anthony Grist Aug 03 '15 at 09:46
  • 1
    Yeah, OP has made a simple mistake - those numbers must surely be `1e0`, `1e1` and `1e11`. – Jamiec Aug 03 '15 at 09:48
  • http://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – Jamiec Aug 03 '15 at 09:51
  • @Jamiec - Sorry I made mistake in representation of scientific number. You are correct. – Pramod S. Nikam Aug 03 '15 at 09:55
  • 1
    @Orion - protip: Thats the point at which you go back and edit your question (ive done it for you!). See the answer I linked above, its the answer to your question too I think. – Jamiec Aug 03 '15 at 09:56

1 Answers1

-1
var temp = number.split('.');
if (typeof temp[1] !== 'undefined') {
  console.log(temp[1].length); // here you got it
} else {
 // 0
}
atinder
  • 2,080
  • 13
  • 15