Remove commas from an input js?

1

Cordial Greeting.

I tell you what I want to do:

I have an input that captures a value: example: 500,000 I capture that input with Js, and I want to remove the comma to make some calculations.

How can I do it?

       var  TotalDevengado = $("#TotalDevengado").val();
          inicio = ",",
          subCadena = TotalDevengado.substring(inicio);
          alert(subCadena)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="TotalDevengado" value="500,000"/>
    
asked by Andres Rodriguez 26.10.2018 в 15:25
source

4 answers

3

You can try using replace() like this:

var TotalDevengado = "500,000,000,000";
TotalDevengado = TotalDevengado.replace(/,/g, "");
alert(TotalDevengado)
   
    
answered by 26.10.2018 / 15:36
source
1

What you need is not to remove commas, what you have to do is an explicit conversion to the amount you have in TotalDevengado because if you had amounts with decimal values such as 15,55 remove the commas the only thing What I would do is return 1555 a totally different amount than expected, for this you can use parseFloat ()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <title>Document</title>
</head>
<body>
    <input type="text" id="TotalDevengado">
    <button onclick=calcular()>calcular</button >
</body>
<script>
  function calcular() {
    var  TotalDevengado = $("#TotalDevengado").val();
    var totalDevengadoFloat = parseFloat(TotalDevengado);
    console.log(totalDevengadoFloat);
    }
</script>
</html>
    
answered by 26.10.2018 в 16:10
0

In javascript the replace function exists to change a part of a string, in this case the character ',' by none ''. The problem is that in principle only the first appearance changes. You have to iterate until you "clean" the entire string.

Code solution

function cleanChar(str, char) {
    console.log('cleanChar()'); // HACK: trace
    while (true) {
        var result_1 = str.replace(char, '');
        if (result_1 === str) {
            break;
        }
        str = result_1;
    }
    return str;
}
var str = '100,000,000';
var result = cleanChar(str, ',');
console.log('Origin: ' + str + ' | Result: ' + result);

Result

cleanChar()
VM124:14 Origin: 100,000,000 | Result: 100000000
    
answered by 26.10.2018 в 15:53
0

This code is what works for me:

String.prototype.toNum = function () {
    return parseFloat(this.split(' ').join('').split(',').join('') || 0);
}

You can even make an alternative to integers:

String.prototype.toInt = function () {
    return parseInt(this.split(' ').join('').split(',').join('') || 0);
}

Basically what it does is that it eliminates the white spaces and the commas, and later it "parsea" to number. The way to use it is like this:

String.prototype.toInt = function (){    
    return parseInt(this.split(' ').join('').split(',').join('') || 0);
}

// Incluso pensándolo como algo más genérico:

toInt = function(val){
  var result;
  if (typeof val === "string")
    result = parseInt(val.split(' ').join('').split(',').join('') || 0);
  else if (typeof val === "number")
    result = parseInt(val);
  else if (typeof val === "object")
    result = 0;
  return result;
}

var TotalDevengado = "500,000".toInt();
var TotalDevengado2 = toInt("500,000");
console.log({TotalDevengado, TotalDevengado2});
    
answered by 26.10.2018 в 16:06