2

I have a view that output decimal values that represent money data types on the database. To output I am using the following formatting code

string price = String.Format("{0:f}", (item.Price + item.VAT));

that produce a string like this

12,99 (with comma as the decimal separator)

I am now using this string to make some calculation on the client using jQuery

elmProductSelected.children("option").each(function(n) {
    var pIdx = $(this).attr("rel"); 
    var pObj = products.eq(pIdx);
    var pValue = $(this).attr("value");
    var valueArray = pValue.split('|');
    var prdId = valueArray[0];
    var pQty = valueArray[1];
    /* the value of pPrice is 12,99 after the following call */
    var pPrice =  $(pObj).attr(attrProductPrice);
    /* I get NaN here! */
    sTotal = sTotal + ((pPrice - 0) * (pQty - 0));
    cProductCount++;
    cItemCount = cItemCount + (pQty-0);
});

I am getting NaN right on the line I have commented. I suppose that this is due to the fact that the pPrice value use the comma as the decimal separator because if I manually set it as 12.99 everything works.

Is there a way to read the 12,99 as a number using javascript/jQuery?

6 Answers 6

3

NaN = Not a number

By using parseInt you can tell Javascript that it should be treated as a number.

var pPrice = parseInt($(pObj).attr(attrProductPrice));

You might need parseFloat if you're using a decimal.

Sign up to request clarification or add additional context in comments.

2 Comments

Both parseInt and parseFloat will cause you to lose the decimal places because of the ,. Example. You'll need to manage the comma first.
You should always use the radix parameter with parseInt. parseInt('0012') -> 10, parseInt('0012', 10) -> 12.
3

There are two problems with the code. First, you should cast the string to float, as Webnet suggests. The parseFloat function, however, will expect the argument to use dot as a decimal separator. Therefore, the following code will do what you want:

var pPrice = $(pObj).attr(attrProductPrice);
pPrice = parseFloat(pPrice.replace(',', '.'));

First, you replace the comma for a dot, then convert the result to a float.

Comments

1

If that's the only comma separator there will be (none for a thousands separator), then just replace it.

var num = parseInt( str.replace( ',', '.' ) ); // or parseFloat

If there are other characters that will prevent correct parsing, then I'd suggest having a custom attribute that stores the undecorated number.

<input data-undecorated="123.45" value="123,45" />
var num = parseInt( $('#myInput').data('undecorated') ); // or parseFloat

You can use data() like this to get data- attributes if you're using jQuery 1.4.3 or later.

2 Comments

I think it should be parseFloat instead of parseInt
@Rafael: Very likely. I have it noted in the code comment. That wasn't the main point of the answer, but worth noting.
1

try this

var pPrice = $(pObj).attr(attrProductPrice).split(",").join(".");

Comments

1

Try this RegEx:

Update: Changed the expression, since I missed the part(with comma as the decimal separator).

var pPrice = $(pObj).attr(attrProductPrice)..replace(/(,)(\d+)$/, ".$2").replace(",","")

Comments

0

This looks related: How to convert string into float in javascript?

It looks like you'll have to replace the comma with a decimal.

parseFloat('12,99'.replace(',', '.'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.