Reseteo Numerico De Api Ja

Páginas: 14 (3359 palabras) Publicado: 27 de mayo de 2012
va * Java tiene una podersa API para formatear numeros en todos los gustos y colores. DecimalFormat es la clase estrella la cual nos permite mostrar nuestros numeros a gusto y placer.
Vamos a mostrar varios ejemplos con numeros enteros y decimales. Basicamente el constructor de DecimalFormat recive un pattern el cual va a servir como base para el formateo.
Aca el ejemplo de agregado deceros.
DecimalFormat format = new DecimalFormat("00000");
format.format(1) /// esto muestra 00001
Una variacion del ejemplo anterior.
DecimalFormat format = new DecimalFormat("00000.00");
format.format(1.19d) /// esto muestra 00001.19
En este mostramos como formatear un monto. Los caracteres # son solo rellenados si hayvalores que los ocupan. Veamos el ejemplo.
DecimalFormat format = new DecimalFormat("#######.##");
format.format(23.32322332) /// esto muestra 23.32
Una variacion con el string $ para que se vea lindo para nuestros lusers
DecimalFormat format = new DecimalFormat("'$'#######.##");
format.format(23.32322332) /// esto muestra$23.32
Aca pueden encontrar el mas completo tutorial oficial de SUN
Customizing Formats
You can use the DecimalFormat class to format decimal numbers into locale-specific strings. This class allows you to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. If you want to change formatting symbols, such as thedecimal separator, you can use the DecimalFormatSymbols in conjunction with the DecimalFormat class. These classes offer a great deal of flexibility in the formatting of numbers, but they can make your code more complex.
The text that follows uses examples that demonstrate the DecimalFormat and DecimalFormatSymbols classes. The code examples in this material are from a sample program calledDecimalFormatDemo.
Constructing Patterns
You specify the formatting properties of DecimalFormat with a pattern String. The pattern determines what the formatted number looks like. For a full description of the pattern syntax, see Number Format Pattern Syntax.
The example that follows creates a formatter by passing a pattern String to the DecimalFormat constructor. The format method accepts a doublevalue as an argument and returns the formatted number in a String:
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
The output for the preceding lines of code is described in the following table. The value is the number, a double , that isto be formatted. The pattern is the String that specifies the formatting properties. The output, which is a String, represents the formatted number.
Output from DecimalFormatDemo Program |
value | pattern | output | Explanation |
123456.789 | ###,###.### | 123,456.789 | The pound sign (#) denotes a digit, the comma is a placeholder for the grouping separator, and the period is a placeholderfor the decimal separator. |
123456.789 | ###.## | 123456.79 | The value has three digits to the right of the decimal point, but the pattern has only two. The format method handles this by rounding up. |
123.78 | 000000.000 | 000123.780 | The pattern specifies leading and trailing zeros, because the 0 character is used instead of the pound sign (#). |
12345.67 | $###,###.### | $12,345.67 |The first character in the pattern is the dollar sign ($). Note that it immediately precedes the leftmost digit in the formatted output. |
12345.67 | \u00A5###,###.### | ¥12,345.67 | The pattern specifies the currency sign for Japanese yen (¥) with the Unicode value 00A5. |
Locale-Sensitive Formatting
The preceding example created a DecimalFormat object for the default Locale. If you want a...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • JA JA JA JA
  • Ja ja ja ja
  • La felicidad ja ja ja
  • la felicidad ja ja ja ja
  • Ja Ja
  • Ja ja
  • Ja Ja Ja
  • La Felicidad Ja Ja Ja

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS