| Primitive Type Conversions | Java1.1 | ||||||||
| To | boolean | char | byte | short | int | long | float | double | |
| from | |||||||||
| boolean | - | n/a | n/a | n/a | n/a | n/a | n/a | n/a | |
| char | n/a | - | cast | = | = | = | = | = | |
| byte | n/a | cast | - | = | = | = | = | = | |
| short | n/a | cast | cast | - | = | = | = | = | |
| int | n/a | cast | cast | cast | - | = | = | = | |
| long | n/a | cast | cast | cast | cast | - | = | = | |
| float | n/a | cast | cast | cast | cast | cast | - | = | |
| double | n/a | cast | cast | cast | cast | cast | cast | - | |
| Type | To String | From String | s <- string | ||||||
| boolean | new Boolean(val).toString() | new Boolean(s).booleanValue() | val <- variable | ||||||
| char | String.valueOf(val) | s.charAt(0) | |||||||
| byte | Byte.toString(val) | Byte.parseByte(s) | |||||||
| short | Short.toString(val) | Short.parseShort(s) | |||||||
| int | Integer.toString(val) | Integer.parseInt(s) | |||||||
| long | Long.toString(val) | Long.parseLong(s) | |||||||
| float | * | Float.toString(val) | new Float(s).floatValue() | ||||||
| double | * | Double.toString(val) | new Double(s).doubleValue() | ||||||
| * preferred method involves Decimal Format | |||||||||
| Symbol | Meaning | Examples | |||||||
| 0 | a digit | Consider | 10.7 | -10000.817 | |||||
| # | a digit, zero shows as absent | ||||||||
| . | placeholder for decimal separator | Format | Result | ||||||
| , | placeholder for grouping separator. | 000 | 010 | -10000 | |||||
| ; | separates formats. | 000.00 | 010.70 | -10000.82 | |||||
| - | default negative prefix. | ###,###.00 | 10.70 | -10,000.82 | |||||
| % | divide by 100 and show as percentage | -^#### | 10 | ^10000 | |||||
| X | any other characters can be used in the prefix or suffix | ||||||||
| ' | used to quote special characters in a prefix or suffix. | ||||||||
| Usage | |||||||||
| s = new DecimalFormat("###,##0.00").format(val) | |||||||||