UNIDAD 1: ELEMENTOS BASICOS

JSCRIPT TEMA 12: APLICACIONES(2)

Un problema interesante es el formateo de datos numericos, para resolver este problema se usara el mismo ejemplo anterior con algunos cambios en el programa executable.

Prog1.js

 
import System;
 
//declarando variables
 
var base1:Double, altura:Double, area:Double;
 
//creando un objeto de captura
 
var capturar:lnet =new lnet();
 
// capturando,cargando y convirtiendo
 
//los datos de la forma a las variables
 
base1 =capturar.getparametro("BASE1");
 
altura =capturar.getparametro("ALTURA");
 
// operaciones
 
area = base1 * altura / 2;
 
//construir la pagina de salida
 
print("Content-Type:text/html\n");
 
print("<html>" );
 
print("base = " +base1 +"<br>" );
 
print("altura = " +altura +"<br>" );
 
print("area = " +area.ToString("#.##") +"<br>" );
 
print("</html>" );

Observar que ahora en la declaración de variables, se estan creando variables ya con un tipo de dato definido, es mucho mejor y mas segura esta tipo de programación.

Al tener la variable con un tipo de dato ya definido, observar que ya no se ocupa hacer un parse(conversión) del valor string que llega de la forma.html.

Sin embargo es importante observar que al compilar este programa, el compilador marca un warning(advertencia) indcando que conversión o parse de string a numero u objeto puede no ser algo seguro y ademas muy lento, por eso es mejor que aunque se hayan declarado las variables con un tipo de dato asignado de todos modos hacer un parse en capturar.getparametro(string) como en el programa anterior.

Corrida prog1.html

image425.jpg

corrida prog1.exe

image426.jpg

Como se observa en la corrida la variable numerica alfa, esta formateada a dos decimales.

Este es el otro cambio que se da en el programa donde se agrega a la variable area el metodo ToString(”#.##”) este metodo lleva adentro lo que se conoce como una string de formato, que es una serie de caracteres especiales que le indican al compilador que formatee la salida, estos caracteres espeeciales son:

Format character Name Description
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.
, Thousand separator and number scaling The ',' character serves two purposes. First, if the format string contains a ',' character between two digit placeholders (0 or #) and to the left of the decimal point if one is present, then the output will have thousand separators inserted between each group of three digits to the left of the decimal separator. The actual character used as the decimal separator in the output string is determined by the NumberGroupSeparator property of the current NumberFormatInfo object that controls formatting. Second, if the format string contains one or more ',' characters immediately to the left of the decimal point, then the number will be divided by the number of ',' characters multiplied by 1000 before it is formatted. For example, the format string '0,,' will represent 100 million as simply 100. Use of the ',' character to indicate scaling does not include thousand separators in the formatted number. Thus, to scale a number by 1 million and insert thousand separators you would use the format string '#,##0,,'.
% Percentage placeholder The presence of a '%' character in a format string causes a number to be multiplied by 100 before it is formatted. The appropriate symbol is inserted in the number itself at the location where the '%' appears in the format string. The percent character used is dependent on the current NumberFormatInfo class.
E0 E+0 E-0 e0 e+0 e-0 Scientific notation If any of the strings 'E', 'E+', 'E-', 'e', 'e+', or 'e-' are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The 'E+' and 'e+' formats indicate that a sign character (plus or minus) should always precede the exponent. The 'E', 'E-', 'e', or 'e-' formats indicate that a sign character should only precede negative exponents.
\ Escape character In C# and the Managed Extensions for C++, the backslash character causes the next character in the format string to be interpreted as an escape sequence. It is used with traditional formatting sequences like “\n” (new line). In some languages, the escape character itself must be preceded by an escape character when used as a literal. Otherwise, the compiler interprets the character as an escape sequence. Use the string “\\” to display “\”. Note that this escape character is not supported in Visual Basic; however, ControlChars provides the same functionality.
'ABC' “ABC” Literal string Characters enclosed in single or double quotes are copied to the output string literally, and do not affect formatting.
; Section separator The ';' character is used to separate sections for positive, negative, and zero numbers in the format string.
Other All other characters All other characters are copied to the output string as literals in the position they appear

Recuerden que en su sitio deberan estar los tres programas siguientes, prog1.html, prog1.exe y lnet.dll.

PROBLEMAS JSCRIPT PROPUESTOS:

CONVERTIR A PROGRAMAS LA OTRA MITAD DE LOS PROBLEMAS CONSTRUIDOS CON EL MODELO DE SOLUCION PARA EL CASO DE QUE NO EXISTA CAPTURA, SOLO CONTRUIR LA PAGINA DE REFERENCIA QUE DESPIERTE EL PROG.EXE APROPIADO

Politica de Privacidad