Como hago para que el resultado salga también con separador de miles (punto)
Publicado por Eduardo (186 intervenciones) el 06/02/2020 20:29:10
Hola a Todos tengo este script que me calcula el 15% y el 8% de un valor en el instante el valor si notan se separa en mil (3 cifras) pero en los resultados no aparece el separador
por ejemplo si pongo
45.000 -> en 15% = 6.000
45.000 -> en 8% = 3.000
pero los valores 6000 y 3000 salen sin el separador de mil
por ejemplo si pongo
45.000 -> en 15% = 6.000
45.000 -> en 8% = 3.000
pero los valores 6000 y 3000 salen sin el separador de mil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<html>
<head>
<script>
function onKeyPressBlockChars(e,numero){
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
reg = /\d|\./;
if (numero.indexOf(".")!=-1 && keychar=="."){
return false;
}else{
return reg.test(keychar);
}
}
function calculaPorcentajes(numero){
document.getElementById("porcent8").value=Math.floor(numero*0.08/1000)*1000;
document.getElementById("porcent15").value=Math.floor(numero*0.15/1000)*1000;
}
//-----SCRIPT SEPARADOR DE MILES---------
function format(input)
{
var num = input.value.replace(/\./g,'');
if(!isNaN(num)){
num = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1.');
num = num.split('').reverse().join('').replace(/^[\.]/,'');
input.value = num;
}
//-- ALERTA SOLO NUMEROS
else{ alert('Solo se permiten numeros');
input.value = input.value.replace(/[^\d\.]*/g,'');
}
}
</script>
<script type="text/javascript">
function sinpuntos( evt )
{
if ( window.event ) { // IE
keyNum = evt.keyCode;
} else {
keyNum = evt.which;
}
if ( keyNum >= 48 && keyNum <= 57 ) {
return true;
} else {
return false;
}
}
</script>
</head>
<body>
Cantidad: <input type="text" name="cantidad" onKeyPress="return sinpuntos(event)" onKeyUp="calculaPorcentajes(this.value)" onChange="format(this)"><br><br><br>
15%: <input name="porcent15" type="text" id="porcent15" onChange="format(this)" onKeyUp="format(this)" readonly><br><br>
8%:
<input name="porcent8" type="text" id="porcent8" onChange="format(this)" onKeyUp="format(this)" readonly>
</body>
</html>
Valora esta pregunta


0