JavaScript - Redondeo al Múltiplo de mil mas cercano

 
Vista:
Imágen de perfil de Eduardo
Val: 159
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Redondeo al Múltiplo de mil mas cercano

Publicado por Eduardo (176 intervenciones) el 07/11/2019 21:35:19
Hola este Script saca los porcentajes pero lo que necesito es que los resultados sean aproximados o redondeados a los múltiplos de mil mas cercanos por ejemplo

al colocar 13000 el valor para el 15% es 1950 debería salir 2000 y así para el mismo valor pero para el 8% que es 1040 debería salir 1000 aca pongo el Script

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
<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.round(numero*8)/100;
	document.getElementById("porcent15").value=Math.round(numero*15)/100;
}
//-----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>
</head>
<body>
Cantidad: <input type="text" name="cantidad" onKeyPress="return onKeyPressBlockChars(event,this.value);" onKeyUp="calculaPorcentajes(this.value)"><br><br>
 
8%: <input type="text" name="porcent8" id="porcent8" onChange="format(this)"><br><br>
15%: <input type="text" name="porcent15" id="porcent15" onChange="format(this)"><br><br>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de JESUS DAVID ARIZA ROYETH
Val: 34
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Redondeo al Múltiplo de mil mas cercano

Publicado por JESUS DAVID ARIZA ROYETH (10 intervenciones) el 07/11/2019 21:58:51
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
<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.round(numero*0.08/1000)*1000;
  document.getElementById("porcent15").value=Math.round(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>
</head>
<body>
Cantidad: <input type="text" name="cantidad" onKeyPress="return onKeyPressBlockChars(event,this.value);" onKeyUp="calculaPorcentajes(this.value)"><br><br>
 
8%: <input type="text" name="porcent8" id="porcent8" onChange="format(this)"><br><br>
15%: <input type="text" name="porcent15" id="porcent15" onChange="format(this)"><br><br>
</body>
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar