1

I have code, I use it in jquery

var string = "123,123";
$.trim(",");

Let I show it in source code:

function calculate_total_money() {
sum_money = 0;
$('table#table_product tr.data td#total_money').each(function() {
    var _total_money = $(this).html();
    if (_total_money != '') {
        var total_money = _total_money;
        sum_money += total_money;
    }
});

$('table#table_product td#sum_total_money').html(formatNumber2(sum_money));} 

This function return total money, but value of _total_money I get in table like that "23,123", but I want to convert it to Int 23123. I don't know how to do.

I want to delete comma in string, but it doesn't work. After delete comma, string must convert to Interger. I try many times but don't have anything happen. where's place I do wrong ?

7 Answers 7

3

If you have single , inside string use replace:

string = parseInt(string.replace(',', ''), 10);

NOTE: replace will only replace the first occurrence of ,.

If you have multiple , inside string use regex in replace:

string = parseInt(string.replace(/,/g, ''), 10);

parseInt to convert string to integer.

Demo

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

Comments

1

Try this:

var string = "123,123";
var str = parseInt(string.replace(",",""));

JSFIDDLE DEMO

Comments

0

trim only removes characters from beginning or end of string, not the middle.

use replace instead.

1 Comment

Thanh you very much !
0

try this..

var string = "123,123";
var val1 = parseInt(string.split(',')[0]);
var val2 = parseInt(string.split(',')[1]);

1 Comment

Thanh you very much !
0

Try this:

var string = "123,123";

parseInt(string.replace(",", ""));

1 Comment

Thanh you very much !
0

use replace() in javascript to remove the , then you have to convert string into number using + operator or parseInt()

    var string = "123,123";
    string=+string.replace(",",""); //+ is used for convert string into number or use parseInt()
   // alert(string)
    //alert(typeof string  )

DEMO

1 Comment

Thanh you very much !
0

Try utilizing String.prototype.match , RegExp /\d+/g/ , Array.prototype.join()

var string = "123,123", n = Number(string.match(/\d+/g).join("")); console.log(n)

1 Comment

Thanh you very much !

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.