# How to calculate this number with comma using javascript ? [Answered]RSS

## 10 replies

Last post Apr 19, 2016 12:52 PM by bertanyadong

Member

49 Points

147 Posts

### How to calculate this number with comma using javascript ?

Hi guys ...
How to calculate this number using javascript ?
I want my total is = 100010,23

Var a : 100000
Var b : 10,23

Total = 100010,23

Thanks.

• Nataraj Gand...

Participant

1911 Points

482 Posts

### Re: How to calculate this number with comma using javascript ?

Apr 16, 2016 02:01 PM|Nataraj Gandhi Arunachalam|LINK

Please try replacing the comma with a dot and then do the arithmetic operation. After that replace the dot with comma again.

var a = "100000";
var b = "10,23";

a = a.replace(',', '.');
b = b.replace(',', '.');

var c = a + b;
c = c.replace('.', ',');

https://jsfiddle.net/t5dtczjf/1/

Thanks and Regards,
Nataraj
----------------------

Member

49 Points

147 Posts

### Re: How to calculate this number with comma using javascript ?

Thanks nataraj.
But that not working in my project. I got wrong result in my project.
My code is :

Var a = 1000;
Var b = 10.5;
Var total = a+b ;

Result = 100010.5

Why my result is 100010.5 not 1010.5 ?

Whats wrong in my project.

Thanks.
• mgebhard

All-Star

38721 Points

15899 Posts

### Re: How to calculate this number with comma using javascript ?

First, the code snippet above will produce a syntax error.

The following produces the correct expected result of 1010.5.  I assume the posted code is not the actual code.

```var a = 1000;
var b = 10.5;
var total = a+b;```

The posted result is string concatenation; "1000" + "10.5" = "100010.5".  Consider using parseInt to convert the values before doing the addition.

Member

49 Points

147 Posts

### Re: How to calculate this number with comma using javascript ?

hi mgebhard.

thanks for reply my thread, but that still not working, this is my query :

price = 9500;

quantity = 10.15

total = (parseInt(price) + parseInt(quantity))  -- > i want my total returning this : 9510,15 ...  but using parseInt  my total showing :  9501

please tell me whats wrong with my code below ?

```var price, quantity, total;

price= document.getElementById('<%=price.ClientID%>');
quantity= document.getElementById('<%=quantity.ClientID%>');

if (!price) {
price= 0;
}
else {
price= price.value.replace(/\,/g, '.');
}

if (!quantity) {
quantity= 0;
}
else {
quantity= quantity.value.replace(/\,/g, '.');
}

//calculate price and quantity

var total= (parseInt(price) +  parseInt(quantity));

}```

• raju dasa

All-Star

15176 Points

3888 Posts

### Re: How to calculate this number with comma using javascript ?

Apr 17, 2016 06:33 AM|raju dasa|LINK

Hi,

Try this code:

```var price, quantity, total;

price= document.getElementById('<%=price.ClientID%>');
quantity= document.getElementById('<%=quantity.ClientID%>');

if (!price) {
price= 0;
}

else {
price= price.value.replace(/\,/g, '.');
}

if (!quantity) {
quantity= 0;
}

else {
quantity= quantity.value.replace(/\,/g, '.');
}

//calculate price and quantity

var total= (parseFloat(price,10) +  parseFloat(quantity,10));

Happy Coding ! Mark as Answer if it helps.
rajudasa.blogspot.com  ||  rajudasa-tech

Member

49 Points

147 Posts

### Re: How to calculate this number with comma using javascript ?

thanks Raju, but still not working.

i've try your code like below :

```var total= (parseFloat(price,10) +  parseFloat(quantity,10));
```

price = 9500;

quantity = 10.15

total = (parseInt(price) + parseInt(quantity))  -- > i want my total returning this : 9510,15 ...  but using (parseInt(price , 10 ) my total showing :  9501

• mgebhard

All-Star

38721 Points

15899 Posts

### Re: How to calculate this number with comma using javascript ?

Sorry, use parseFloat not parseInt.  Then replace the decimal with a comma.

Member

49 Points

147 Posts

### Re: How to calculate this number with comma using javascript ?

Hi mgebhard.
What your mean about replace the decimal with a comma ? Thanks
• Fei Han - MS...

All-Star

39315 Points

5751 Posts

Microsoft

### Re: How to calculate this number with comma using javascript ?

Apr 19, 2016 10:47 AM|Fei Han - MSFT|LINK

i want my total returning this : 9510,15 ...  but using (parseInt(price , 10 ) my total showing :  9501

As others said, you could use parseFloat() function to parse a string before you do calculation. The following sample is for your reference.

```var price = parseFloat("9500");

var quantity = parseFloat("10.15");

var total = price + quantity;

//if you'd like to replace . with ,
total = total.toString().replace(/\./g, ',');

```

Best Regards,

Fei Han

MSDN Community Support

Member

49 Points

147 Posts