Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to display a value to 2 decimal places in an input box while retaining more precision [duplicate]

Possible Duplicate:
JavaScript: formatting number with exactly two decimals

Maybe I'm going about this the wrong way, but I have some javascript which is filling up some text inputs with numbers like 12.434234234. I want the text input to only display 12.43, but I'd like it's value to remain as 12.434234234.

Is this even possible? If not, what would be the best work-around?

Thanks! John.

--

Although there were some very good answers here, I was looking for something like 'why don't you just use this clever one liner thing..' - hence I've ticked the most basic approach which is to just use an extra field. Thanks though! Marked up all answers.

like image 378
John Hunt Avatar asked Mar 16 '11 01:03

John Hunt


2 Answers

num = 12.434234234;
result = num.toFixed(2); // returns 12.43
like image 120
Mike Lewis Avatar answered Sep 19 '22 11:09

Mike Lewis


You can store the fine-grained values in hidden fields, and trim the ones displayed for the user.

like image 32
weltraumpirat Avatar answered Sep 17 '22 11:09

weltraumpirat