Foros del Web » Programando para Internet » Javascript »

Hola, ayudenme a utilizar esta inst. jscript. basic

Estas en el tema de Hola, ayudenme a utilizar esta inst. jscript. basic en el foro de Javascript en Foros del Web. Este es el codigo en el cual necesito que todos los resultados se muestren con dos decimales.. creo que es con tostring... pero no sé ...
  #1 (permalink)  
Antiguo 26/01/2005, 15:46
 
Fecha de Ingreso: enero-2005
Mensajes: 3
Antigüedad: 19 años, 3 meses
Puntos: 0
Exclamación Hola, ayudenme a utilizar esta inst. jscript. basic

Este es el codigo en el cual necesito que todos los resultados se muestren con dos decimales.. creo que es con tostring... pero no sé como usarlo,,, ayudenme por favor...





<SCRIPT language=JavaScript>
<!--
function calculo2 (num) {
if (num <=2)
(document.f1.res.value =74.75)
if (num >2 && num <=20)
(document.f1.res.value =(((num-2)*5.5)+65)*(.15)+(((num-2)*5.5)+65))
if (num >20 && num <=100)
(document.f1.res.value =(((num-2)*5)+74)*(.15)+(((num-2)*5)+74))
if (num >100 && num <=200)
(document.f1.res.value =(((num-2)*4.5)+123)*(.15)+(((num-2)*4.5)+123))
if (num >200)
(document.f1.res.value =(((num-2)*4)+222)*(.15)+(((num-2)*4)+222))
}
//-->

</SCRIPT>

MIREN.. ESTE ES EL CODIGO QUE NECESITO INCRUSTAR (TO.STRING) PERO NO SE COMO PONERLO EN EL CODIGO DE ARRIBA.. OJALA PUEDAN ECHARME LA MANO.

<SCRIPT LANGUAGE=JSCRIPT RUNAT=SERVER>

function CALCULO(sender:Object, e:EventArgs):void

{

AREA.Text = BASE1.Text * ALTURA.Text / 2;

// formateando la salida

AREA.Text = Double.Parse(AREA.Text).ToString("#.##");
}

</script>




Al final ya cargado el resultado (AREA), se esta realizando una operación extra, es decir se esta formateando, lo que se pretende es que solo aparezcan dos decimales en la salida, como se obserava del ejemplo primero el AREA.TEXT se convierte a DOUBLE y como tal ya puede usar la instrucción ToString(string de formato), esta string de formato puede contener los siguientes caracteres:


Format character
Name
Description

0
Zero placeholder
If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the output string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the output string.

#
Digit placeholder
If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string. Note that this specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed.

.
Decimal point
The first '.' character in the format string determines the location of the decimal separator in the formatted value; any additional '.' characters are ignored. The actual character used as the decimal separator is determined by the NumberDecimalSeparator property of the NumberFormatInfo object that controls formatting.
  #2 (permalink)  
Antiguo 26/01/2005, 18:25
 
Fecha de Ingreso: octubre-2004
Ubicación: España
Mensajes: 894
Antigüedad: 19 años, 6 meses
Puntos: 3
Hola
Hay que buscar en el foro. Justo en un mensaje de ayer está la solución:
http://www.forosdelweb.com/showthrea...ight=decimales
__________________
Angel :cool:
  #3 (permalink)  
Antiguo 28/01/2005, 15:24
 
Fecha de Ingreso: enero-2005
Mensajes: 3
Antigüedad: 19 años, 3 meses
Puntos: 0
Gracias amigo,, pero el problema es que no se como incrustar el formato de texto a mi codigo, soy nuevo en lo de los jscripts y en verdad necesito pronto hacer que se de el resultado con dos decimales porque es un cotizador, ojala puedas echarme la mano porque ya lo intente pero no le hayo el modo...
  #4 (permalink)  
Antiguo 30/01/2005, 11:07
 
Fecha de Ingreso: octubre-2004
Ubicación: España
Mensajes: 894
Antigüedad: 19 años, 6 meses
Puntos: 3
Ravenet, no sé exactamente en qué código lo quieres meter, pero mira a ver si con esto te vale:
Código HTML:
<html>
<head>
<script language="javascript" type="text/javascript">
function decimales(Numero, Decimales) {
	pot = Math.pow(10,Decimales);
	num = parseInt(Numero * pot) / pot;
	nume = num.toString().split('.');

	entero = nume[0];
	decima = nume[1];

	if (decima != undefined) {
		fin = Decimales-decima.length; }
	else {
		decima = '';
		fin = Decimales; }

	for(i=0;i<fin;i++)
	  decima+=String.fromCharCode(48); 

	num=entero+'.'+decima;
	return num;
}

function calculo2 (num) {
if (num <=2) 
  { resul =74.75 }
else if (num <=20)
  { resul =((num-2)*5.5+65)*.15+((num-2)*5.5+65) }
else if (num <=100)
  { resul =((num-2)*5+74)*.15+((num-2)*5+74) }
else if (num <=200)
  { resul =((num-2)*4.5+123)*.15+((num-2)*4.5+123) }
else 
  { resul =((num-2)*4+222)*.15+((num-2)*4+222) }

document.f1.res.value = decimales(resul,2)
}

</script>
</head>

<body>

<form name="f1">
<input type="text" name="dato">
<input type="button" value="Calcular" onClick="calculo2(this.form.dato.value)">
<input type="text" name="res">
</form>

</body>
</html> 
__________________
Angel :cool:
  #5 (permalink)  
Antiguo 31/01/2005, 14:50
 
Fecha de Ingreso: enero-2005
Mensajes: 3
Antigüedad: 19 años, 3 meses
Puntos: 0
Muchas Gracias Amigo!!!!!!!!!!!!!!!!!!!!!!! Te Debo Una!!!!!!!!!!!!! Gracias!!!!!!!
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:09.