0

My code:

var $label = $(ev.currentTarget);
var $price = $label.parent("form").find(".oe_price .oe_currency_value");

if (!$price.data("price")) {
    $price.data("price", parseFloat($price.text());   //price is 10.30 but it returns 10.3 as number
}

$price.html($price.data("price")+parseFloat($label.find(".badge span").text() || 0));
/* The value coming in **badge** is **12.00**
 * but parseFloat converts that into **12**
 * thats why after the addition I got the output as **22.3**
 * but i want it as **22.30**
 */

I have a string

'10.30'

Now, if I convert the string to number using parsefloat

parseFloat('10.30')

I got the output as 10.3

And If I do it using the .tofixed(2)

parseFloat('10.30').toFixed(2)

I got the output 10.30 but it is in STRING which is the big problem for me because I want the output as number.

And if i do like this

Number(parseFloat('10.30').toFixed(2))

I got the output 10.3

But i want the output in number and with decimal point like this 10.30

Plz help...!!!

Passerby
  • 9,715
  • 2
  • 33
  • 50
ParaMeterz
  • 9,507
  • 1
  • 19
  • 21
  • 1
    The number `10.3` is precisely the same as `10.30` so why does it matter, numerically? If it's for presentation in a web page then a string is fine. – David Thomas Feb 06 '14 at 11:08
  • The last digit is a 0, its irrelevant for floating point calculations to have it included. When you output something, it becomes a string anyway, so what's the problem exactly? – gpgekko Feb 06 '14 at 11:08
  • Please Check out http://stackoverflow.com/questions/2221167/javascript-formatting-a-rounded-number-to-n-decimals/2909252#2909252 – Akhil Sidharth Feb 06 '14 at 11:09
  • @DavidThomas ::> Ya, I knw that the last digit is zero. But, in my case its very much important. Its for the ecommerce website and in product price I have to display the decimal. – ParaMeterz Feb 06 '14 at 11:11
  • Also, using floats for prices might not be the best idea. – WGH Feb 06 '14 at 11:14
  • Please demonstrate a case where string is inpropriate (especially you said you need to use it in **display**) and a zero-padded number is necessary. – Passerby Feb 06 '14 at 11:15
  • @Passerby Edited the comment by adding my code. Plz have a look and help if u can. :( – ParaMeterz Feb 07 '14 at 04:48
  • @ParaMeterz `$price.data("price",parseFloat($price.text(),10).toFixed(2));` – Passerby Feb 07 '14 at 04:52
  • 1
    @ParaMeterz Or if you only cares about the `.html()` call: `$price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0,10)).toFixed(2));` – Passerby Feb 07 '14 at 04:58
  • @Passerby OMG..!!! thankz thankzzz a lot **Passby** You saved my day. thankz.. – ParaMeterz Feb 07 '14 at 05:18
  • @Passerby I tired that code also but i was doing like this $price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0)).toFixed(2)); I was not placing that **0,10** – ParaMeterz Feb 07 '14 at 05:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46985/discussion-between-parameterz-and-passerby) – ParaMeterz Feb 07 '14 at 05:24

3 Answers3

1

thankz Passerby for the help.

Finally i got the result from this :

$price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0,10)).toFixed(2));

ParaMeterz
  • 9,507
  • 1
  • 19
  • 21
0

Try this:

parseFloat(Math.round(num3 * 100) / 100).toFixed(2);

See Live Demo

Jzf
  • 90
  • 2
  • 9
  • Thankz for help. But I had checked that demo and the code of yours but the output coming is in string. :( But I wanted in number. The situation is this when we use .tofixed() it will convert the number into string. :( – ParaMeterz Feb 06 '14 at 11:18
  • No man it didn't fixed. I want the final output as number not string. If was use .toFixed() it will convert the output to string. – ParaMeterz Feb 06 '14 at 11:25
0

I don't know what are you doing, but I've done the same and got desired result.

My code:

var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);

See here: Fiddle

earthmover
  • 4,395
  • 10
  • 43
  • 74
  • Checkout the last alert man the typeof(flo) is string, but i wanted number. alert(typeof(flo)); – ParaMeterz Feb 06 '14 at 11:23
  • Yes, I saw this...Actually `toFixed()` always converts float to string...check here : http://stackoverflow.com/q/2283566/2764279 – earthmover Feb 06 '14 at 11:29
  • If you want a `number` as `10.30`, than it doesn't matter if it is `10.30` or `10.3`, and if you want to display it somewhere then `String` is fine for that. Where are you actually using this??? – earthmover Feb 06 '14 at 11:42
  • Ya, I knw that the last digit is zero and doesn't matter. But, in my case its very much important. Its for the product price I have to display the decimal. And in this case I have to add two fields price + discount and both the things are coming as string. So, for that i have to first convert them into number than i can add. Thats why I want the output in number with decimal 0. :( – ParaMeterz Feb 06 '14 at 11:53