Last updated 2004-06-29 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : C words : Conversion 1.5+.
| Convert To | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| to: boolean t | to: /*signed*/ byte b | to: /*unsigned*/ byte u | to: short s | to: char c | to: int i | to: long n | to: float f | to: double d | to: String g | to: Boolean tt | to: Short ss | to: Character cc | to: Integer ii | to: Long nn | to: Float ff | to: Double dd | ||
| From | From | |||||||||||||||||
| boolean t | -------- | // to /*signed*/ byte b from boolean t b = (byte)(t?1:0); |
// to /*unsigned*/ byte u from boolean t u = (byte)(t?1:0); |
// to short s from boolean t s = (short)(t?1:0); |
// to char c from boolean t /* best */ c = (char)(t?'1':'0'); /* or */ c = (char)(t?1:0); |
// to int i from boolean t i = t?1:0; |
// to long n from boolean t n = t?1:0; |
// to float f from boolean t f = t?1:0; |
// to double d from boolean t d = t?1:0; |
// to String g from boolean t g = String.valueOf(t); |
// to Boolean tt from boolean t // no conversion necessary in Java 1.5+ |
// to Short ss from boolean t ss = new Short((short)(t?1:0)); |
// to Character cc from boolean t /* best */ cc = new Character((char)(t?'1':'0')); /* or */ cc = new Character((char)(t?0:1)); |
// to Integer ii from boolean t ii = new Integer(t?1:0); |
// to Long nn from boolean t nn = new Long(t?1:0); |
// to Float ff from boolean t ff = new Float(t?1:0); |
// to Double dd from boolean t dd = new Double(t?1:0); |
boolean t |
| /*signed*/ byte b | // to boolean t from /*signed*/ byte b t = b != 0; |
-------- | // to /*unsigned*/ byte u from /*signed*/ byte b u = b; |
// to short s from /*signed*/ byte b s = b; |
// to char c from /*signed*/ byte b c = (char)b; |
// to int i from /*signed*/ byte b i = b; // sign extends. |
// to long n from /*signed*/ byte b n = b; // sign extends. |
// to float f from /*signed*/ byte b f = b; |
// to double d from /*signed*/ byte b d = b; |
// to String g from /*signed*/ byte b /* best for readability */ g = Integer.toString(b); /* best for maintainablity */ g = String.valueOf(b); /* or */ g = Integer.toString(b, 7 /* radix */); /* or */ g = Integer.toBinaryString(b); /* or */ g = Integer.toOctalString(b); /* or */ g = Integer.toHexString(b); /* or kludgy and slow on unoptimised Javas */ g = "" + b; |
// to Boolean tt from /*signed*/ byte b tt = (b != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from /*signed*/ byte b ss = new Short(b); // sign extends |
// to Character cc from /*signed*/ byte b cc = new Character((char)b); |
// to Integer ii from /*signed*/ byte b ii = new Integer(b); // sign extends. |
// to Long nn from /*signed*/ byte b nn = new Long(b); // sign extends. |
// to Float ff from /*signed*/ byte b ff = new Float(b); // sign extends. |
// to Double dd from /*signed*/ byte b dd = new Double(b); // sign extends. |
/*signed*/ byte b |
| /*unsigned*/ byte u | // to boolean t from /*unsigned*/ byte u t = u != 0; |
// to /*signed*/ byte b from /*unsigned*/ byte u b = u; |
-------- | // to short s from /*unsigned*/ byte u s = (short)(u & 0xff); |
// to char c from /*unsigned*/ byte u c = (char)(u & 0xff); |
// to int i from /*unsigned*/ byte u i = u & 0xff; // does not sign extend |
// to long n from /*unsigned*/ byte u n = ((long)u) & 0xff; // does not sign extend. |
// to float f from /*unsigned*/ byte u f = u & 0xff; // does not sign extend. |
// to double d from /*unsigned*/ byte u d = u & 0xff; // does not sign extend. |
// to String g from /*unsigned*/ byte u /* best for readabilty */ g = Integer.toString(u & 0xff); /* best for maintainablity */ g = String.valueOf(u & 0xff); /* or */ g = Integer.toString(u & 0xff, 7 /* radix */); /* or */ g = Integer.toBinaryString(u & 0xff); /* or */ g = Integer.toOctalString(u & 0xff); /* or */ g = Integer.toHexString(u & 0xff); /* or kludgy and possibly slow */ g = "" + (u & 0xff); |
// to Boolean tt from /*unsigned*/ byte u tt = (u != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from /*unsigned*/ byte u ss = new Short((short)(u & 0xff)); // does not sign extend. |
// to Character cc from /*unsigned*/ byte u cc = new Character((char)(u & 0xff)); |
// to Integer ii from /*unsigned*/ byte u ii = new Integer(u & 0xff); // does not sign extend. |
// to Long nn from /*unsigned*/ byte u nn = new Long(((long)u) & 0xff); // does not sign extend. |
// to Float ff from /*unsigned*/ byte u ff = new Float(((long)u) & 0xff); // does not sign extend. |
// to Double dd from /*unsigned*/ byte u dd = new Double(((long)u) & 0xff); // does not sign extend. |
/*unsigned*/ byte u |
| short s | // to boolean t from short s t = s != 0; |
// to /*signed*/ byte b from short s b = (byte)s; |
// to /*unsigned*/ byte u from short s u = (byte)s; |
-------- | // to char c from short s c = (char)s; |
// to int i from short s // no conversion necessary in Java 1.5+ |
// to long n from short s // no conversion necessary in Java 1.5+ |
// to float f from short s // no conversion necessary in Java 1.5+ |
// to double d from short s // no conversion necessary in Java 1.5+ |
// to String g from short s /* best for readabilty */ g = Integer.toString(s); /* best for maintainablity */ g = String.valueOf(s); /* or */ g = Integer.toString(s, 7 /* radix */); /* or */ g = Integer.toBinaryString(s); /* or */ g = Integer.toOctalString(s); /* or */ g = Integer.toHexString(s); /* or kludgy and possibly slow */ g = "" + s; |
// to Boolean tt from short s tt = (s != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from short s // no conversion necessary in Java 1.5+ |
// to Character cc from short s cc = new Character((char)s); |
// to Integer ii from short s // no conversion necessary in Java 1.5+ |
// to Long nn from short s // no conversion necessary in Java 1.5+ |
// to Float ff from short s // no conversion necessary in Java 1.5+ |
// to Double dd from short s // no conversion necessary in Java 1.5+ |
short s |
| char c | // to boolean t from char c t = c != 0; |
// to /*signed*/ byte b from char c b = (byte)c; |
// to /*unsigned*/ byte u from char c u = (byte)c; |
// to short s from char c /* best */ s = (short)(c - '0'); /* or */ s = (short)c; |
-------- | // to int i from char c /* best */ i = c - '0'; /* or */ i = c; // does not sign extend. |
// to long n from char c /* best */ n = c - '0'; /* or */ n = c; // does not sign extend. |
// to float f from char c f = c; // does not sign extend. |
// to double d from char c d = c; // does not sign extend. |
// to String g from char c g = String.valueOf(c); |
// to Boolean tt from char c /* best */ tt = (c != '0') ? Boolean.TRUE : Boolean.FALSE; /* or */ tt = (c != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from char c ss = new Short((short)c); |
// to Character cc from char c // no conversion necessary in Java 1.5+ |
// to Integer ii from char c ii = new Integer(c); // does not sign extend. |
// to Long nn from char c nn = new Long(c); // does not sign extend. |
// to Float ff from char c ff = new Float(c); // does not sign extend. |
// to Double dd from char c dd = new Double(c); // does not sign extend. |
char c |
| int i | // to boolean t from int i t = i != 0; |
// to /*signed*/ byte b from int i b = (byte)i; |
// to /*unsigned*/ byte u from int i u = (byte)i; |
// to short s from int i s = (short)i; |
// to char c from int i /* best */ c = (char)(i + '0'); /* or */ c = (char)i; |
-------- | // to long n from int i // no conversion necessary in Java 1.5+ |
// to float f from int i // no conversion necessary in Java 1.5+ |
// to double d from int i // no conversion necessary in Java 1.5+ |
// to String g from int i /* best for readabilty */ g = Integer.toString(i); /* best for maintainablity */ g = String.valueOf(i); /* or */ g = Integer.toString(i, 7 /* radix */); /* or */ g = Integer.toBinaryString(i); /* or */ g = Integer.toOctalString(i); /* or */ g = Integer.toHexString(i); /* or kludgy and possibly slow */ g = "" + i; |
// to Boolean tt from int i tt = (i != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from int i ss = new Short((short)i); |
// to Character cc from int i cc = new Character((char)i); |
// to Integer ii from int i // no conversion necessary in Java 1.5+ |
// to Long nn from int i // no conversion necessary in Java 1.5+ |
// to Float ff from int i // no conversion necessary in Java 1.5+ |
// to Double dd from int i // no conversion necessary in Java 1.5+ |
int i |
| long n | // to boolean t from long n t = n != 0; |
// to /*signed*/ byte b from long n b = (byte)n; |
// to /*unsigned*/ byte u from long n u = (byte)n; |
// to short s from long n s = (short)n; |
// to char c from long n /* best */ c = (char)(n + '0'); /* or */ c = (char)n; |
// to int i from long n i = (int)n; |
-------- | // to float f from long n // no conversion necessary in Java 1.5+ |
// to double d from long n // no conversion necessary in Java 1.5+ |
// to String g from long n /* best for readabilty */ g = Long.toString(n); /* best for maintainablity */ g = String.valueOf(n); /* or */ g = Long.toString(n, 7 /* radix */); /* or */ g = Long.toBinaryString(n); /* or */ g = Long.toOctalString(n); /* or */ g = Long.toHexString(n); /* or kludgy and possibly slow */ g = "" + n; |
// to Boolean tt from long n tt = (n != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from long n ss = new Short((short)n); |
// to Character cc from long n cc = new Character((char)n); |
// to Integer ii from long n ii = new Integer((int)n); |
// to Long nn from long n // no conversion necessary in Java 1.5+ |
// to Float ff from long n // no conversion necessary in Java 1.5+ |
// to Double dd from long n // no conversion necessary in Java 1.5+ |
long n |
| float f | // to boolean t from float f t = f != 0; |
// to /*signed*/ byte b from float f b = (byte)f; |
// to /*unsigned*/ byte u from float f u = (byte)f; |
// to short s from float f s = (short)f; |
// to char c from float f c = (char)f; |
// to int i from float f /* best */ i = (int)f; /* or */ i = Math.round(f); /* or */ i = (int)Math.ceil(f); /* or */ i = (int)Math.floor(f); /* to see the IEEE bits inside a float */ i = Float.floatToIntBits(f); |
// to long n from float f /* best */ n = (long)f; /* or */ n = Math.round(f); /* or */ n = (long)Math.ceil(f); /* or */ n = (long)Math.floor(f); |
-------- | // to double d from float f // no conversion necessary in Java 1.5+ |
// to String g from float f /* 2 decimal places, locale-sensitive. */ java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(f); /* or exponential scientific format, locale-sensitive. */ java.text.DecimalFormat de = new java.text.DecimalFormat("0.000000E00"); g = de.format(f); /* or best for readability, no loss of precision, locale-insensitive */ g = Float.toString(f); /* or best for maintainability, no loss of precision, locale-insensitive */ g = String.valueOf(f); |
// to Boolean tt from float f tt = (f != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from float f ss = new Short((short)f); |
// to Character cc from float f cc = new Character((char)f); |
// to Integer ii from float f ii = new Integer((int)f); |
// to Long nn from float f nn = new Long((long)f); |
// to Float ff from float f // no conversion necessary in Java 1.5+ |
// to Double dd from float f // no conversion necessary in Java 1.5+ |
float f |
| double d | // to boolean t from double d t = d != 0; |
// to /*signed*/ byte b from double d b = (byte)d; |
// to /*unsigned*/ byte u from double d u = (byte)d; |
// to short s from double d s = (short)d; |
// to char c from double d c = (char)d; |
// to int i from double d /* best */ i = (int)d; /* or */ i = (int)Math.round(d); /* or */ i = (int)Math.ceil(d); /* or */ i = (int)Math.floor(d); |
// to long n from double d /* best */ n = (long)d; /* or */ n = Math.round(d); /* or */ n = (long)Math.ceil(d); /* or */ n = (long)Math.floor(d); /* to see the IEEE bits inside a double */ n = Double.doubleToLongBits(d); |
// to float f from double d f = (float)d; |
-------- | // to String g from double d /* 2 decimal places, locale-sensitive. */ java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(d); /* or exponential scientific format, locale-sensitive. */ java.text.DecimalFormat de = new java.text.DecimalFormat("0.0000000000E00"); g = de.format(d); /* or best for readability, no loss of precision, locale-insensitive */ g = Double.toString(d); /* or best for maintainability, no loss of precision, locale-insensitive */ g = String.valueOf(d); |
// to Boolean tt from double d tt = (d != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from double d ss = new Short((short)d); |
// to Character cc from double d cc = new Character((char)d); |
// to Integer ii from double d ii = new Integer((int)d); |
// to Long nn from double d nn = new Long((long)d); |
// to Float ff from double d ff = new Float(d); |
// to Double dd from double d // no conversion necessary in Java 1.5+ |
double d |
| String g | // to boolean t from String g t = new Boolean(g.trim()).booleanValue(); /* or */ t = g.trim().equalsIgnoreCase("true"); |
// to /*signed*/ byte b from String g try { /* best */ b = (byte)Integer.parseInt(g.trim()); /* or */ b = (byte)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){} |
// to /*unsigned*/ byte u from String g try { /* best */ u = (byte)Integer.parseInt(g.trim()); /* or */ u = (byte)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){} |
// to short s from String g try { /* best */ s = (short)Integer.parseInt(g.trim()); /* or */ s = (short)Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){} |
// to char c from String g try { /* best */ c = (char)Integer.parseInt(g.trim()); /* or */ c = (char)Integer.parseInt(g.trim(), 16 /* radix */); /* or */ c = g.charAt(0 /* position */); } catch (NumberFormatException e){} |
// to int i from String g try { /* best */ i = Integer.parseInt(g.trim()); /* or */ i = Integer.parseInt(g.trim(), 16 /* radix */); } catch (NumberFormatException e){} |
// to long n from String g try { n = Long.parseLong(g.trim()); } catch (NumberFormatException e){} |
// to float f from String g try { /* best locale-insensitive */ f = Float.parseFloat(g.trim()); /* or locale-insensitive */ f = Float.valueOf(g.trim()).floatValue(); } catch (NumberFormatException e){} |
// to double d from String g try { /* best, locale-insensitive */ d = Double.parseDouble(g.trim()); /* or locale-insensitive */ d = Double.valueOf(g.trim()).doubleValue(); } catch (NumberFormatException e){} |
-------- | // to Boolean tt from String g /* best */ tt = new Boolean(g.trim()); /* or */ tt = Boolean.valueOf(g.trim()); |
// to Short ss from String g try { /* best */ ss = new Short(Short.parseShort(g.trim())); /* or */ ss = new Short(Short.parseShort(g.trim(), 16 /* radix */)); /* or */ ss = new Short((short)g.charAt(0 /* position */)); } catch (NumberFormatException e){} |
// to Character cc from String g try { /* best */ cc = new Character((char)Integer.parseInt(g.trim())); /* or */ cc = new Character((char)Integer.parseInt(g.trim(), 16 /* radix */)); /* or */ cc = new Character(g.charAt(0 /* position */)); } catch (NumberFormatException e){} |
// to Integer ii from String g try { /* best */ ii = new Integer(g.trim()); /* or */ ii = Integer.valueOf(g.trim()); } catch (NumberFormatException e){} |
// to Long nn from String g try { /* best */ nn = new Long(g.trim()); /* or */ nn = Long.valueOf(g.trim()); } catch (NumberFormatException e){} |
// to Float ff from String g try { /* best, locale-insensitive. */ ff = new Float(g.trim()); /* or locale-insensitive */ ff = Float.valueOf(g.trim()); } catch (NumberFormatException e){} |
// to Double dd from String g try { /* bes locale-insensitive*/ dd = new Double(g); /* or locale-insensitive */ dd = Double.valueOf(g); } catch (NumberFormatException e){} |
String g |
| Boolean tt | // to boolean t from Boolean tt // no conversion necessary in Java 1.5+ |
// to /*signed*/ byte b from Boolean tt b = (byte)(tt.booleanValue()?1:0); |
// to /*unsigned*/ byte u from Boolean tt u = (byte)(tt.booleanValue()?1:0); |
// to short s from Boolean tt s = (short)(tt.booleanValue()?1:0); |
// to char c from Boolean tt /* best */ c = (char)(tt.booleanValue()?'1':'0'); /* or */ c = (char)(tt.booleanValue()?0:1); |
// to int i from Boolean tt i = tt.booleanValue()?1:0; |
// to long n from Boolean tt n = tt.booleanValue()?1:0; |
// to float f from Boolean tt f = tt.booleanValue()?1:0; |
// to double d from Boolean tt d = tt.booleanValue()?1:0; |
// to String g from Boolean tt g = tt.toString(); |
-------- | // to Short ss from Boolean tt ss = new Short((short)(tt.booleanValue()?1:0)); |
// to Character cc from Boolean tt /* best */ cc = new Character((char)(tt.booleanValue()?'1':'0')); /* or */ cc = new Character((char)(tt.booleanValue()?1:0)); |
// to Integer ii from Boolean tt ii = new Integer(tt.booleanValue()?1:0); |
// to Long nn from Boolean tt nn = new Long(tt.booleanValue()?1:0); |
// to Float ff from Boolean tt ff = new Float(tt.booleanValue()?1:0); |
// to Double dd from Boolean tt dd = new Double(tt.booleanValue()?1:0); |
Boolean tt |
| Short ss | // to boolean t from Short ss t = ss.shortValue() != 0; |
// to /*signed*/ byte b from Short ss b = (byte)ss.shortValue(); |
// to /*unsigned*/ byte u from Short ss u = (byte)ss.shortValue(); |
// to short s from Short ss // no conversion necessary in Java 1.5+ |
// to char c from Short ss c = (char) ss.shortValue(); |
// to int i from Short ss // no conversion necessary in Java 1.5+ |
// to long n from Short ss // no conversion necessary in Java 1.5+ |
// to float f from Short ss // no conversion necessary in Java 1.5+ |
// to double d from Short ss // no conversion necessary in Java 1.5+ |
// to String g from Short ss g = ss.toString(); |
// to Boolean tt from Short ss /* best */ tt = (ss.shortValue() != '0') ? Boolean.TRUE : Boolean.FALSE; /* or */ tt = (ss.shortValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
-------- | // to Character cc from Short ss cc = new Character((char)ss.shortValue()); |
// to Integer ii from Short ss // no conversion necessary in Java 1.5+ |
// to Long nn from Short ss // no conversion necessary in Java 1.5+ |
// to Float ff from Short ss // no conversion necessary in Java 1.5+ |
// to Double dd from Short ss // no conversion necessary in Java 1.5+ |
Short ss |
| Character cc | // to boolean t from Character cc t = cc.charValue() != 0; |
// to /*signed*/ byte b from Character cc b = (byte)cc.charValue(); |
// to /*unsigned*/ byte u from Character cc u = (byte)cc.charValue(); |
// to short s from Character cc s = (short)cc.charValue(); |
// to char c from Character cc // no conversion necessary in Java 1.5+ |
// to int i from Character cc i = cc.charValue(); // does not sign extend. |
// to long n from Character cc n = cc.charValue(); // does not sign extend. |
// to float f from Character cc f = cc.charValue(); // does not sign extend. |
// to double d from Character cc d = cc.charValue(); // does not sign extend. |
// to String g from Character cc g = cc.toString(); |
// to Boolean tt from Character cc /* best */ tt = (cc.charValue() != '0') ? Boolean.TRUE : Boolean.FALSE; /* or */ tt = (cc.charValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from Character cc ss = new Short((short)cc.charValue()); |
-------- | // to Integer ii from Character cc ii = new Integer(cc.charValue()); |
// to Long nn from Character cc nn = new Long(cc.charValue()); |
// to Float ff from Character cc ff = new Float(cc.charValue()); |
// to Double dd from Character cc dd = new Double(cc.charValue()); |
Character cc |
| Integer ii | // to boolean t from Integer ii t = ii.intValue() != 0; |
// to /*signed*/ byte b from Integer ii b = (byte)ii.intValue(); |
// to /*unsigned*/ byte u from Integer ii u = (byte)ii.intValue(); |
// to short s from Integer ii s = (short)ii.intValue(); |
// to char c from Integer ii c = (char)ii.intValue(); |
// to int i from Integer ii // no conversion necessary in Java 1.5+ |
// to long n from Integer ii // no conversion necessary in Java 1.5+ |
// to float f from Integer ii // no conversion necessary in Java 1.5+ |
// to double d from Integer ii // no conversion necessary in Java 1.5+ |
// to String g from Integer ii g = ii.toString(); |
// to Boolean tt from Integer ii tt = (ii.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from Integer ii ss = new Short(ii.shortValue()); |
// to Character cc from Integer ii cc = new Character((char)ii.intValue()); |
-------- | // to Long nn from Integer ii // no conversion necessary in Java 1.5+ |
// to Float ff from Integer ii // no conversion necessary in Java 1.5+ |
// to Double dd from Integer ii // no conversion necessary in Java 1.5+ |
Integer ii |
| Long nn | // to boolean t from Long nn t = nn.longValue() != 0; |
// to /*signed*/ byte b from Long nn b = (byte)nn.intValue(); |
// to /*unsigned*/ byte u from Long nn u = (byte)nn.intValue(); |
// to short s from Long nn s = (short)nn.intValue(); |
// to char c from Long nn c = (char)nn.intValue(); |
// to int i from Long nn i = nn.intValue(); |
// to long n from Long nn // no conversion necessary in Java 1.5+ |
// to float f from Long nn // no conversion necessary in Java 1.5+ |
// to double d from Long nn // no conversion necessary in Java 1.5+ |
// to String g from Long nn g = nn.toString(); |
// to Boolean tt from Long nn tt = (nn.longValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from Long nn ss = new Short(nn.shortValue()); |
// to Character cc from Long nn cc = new Character((char)nn.intValue()); |
// to Integer ii from Long nn ii = new Integer(nn.intValue()); |
-------- | // to Float ff from Long nn // no conversion necessary in Java 1.5+ |
// to Double dd from Long nn // no conversion necessary in Java 1.5+ |
Long nn |
| Float ff | // to boolean t from Float ff t = ff.floatValue() != 0; |
// to /*signed*/ byte b from Float ff b = (byte)ff.intValue(); |
// to /*unsigned*/ byte u from Float ff u = (byte)ff.intValue(); |
// to short s from Float ff s = (short)ff.intValue(); |
// to char c from Float ff c = (char)ff.intValue(); |
// to int i from Float ff i = ff.intValue(); |
// to long n from Float ff n = ff.longValue(); |
// to float f from Float ff // no conversion necessary in Java 1.5+ |
// to double d from Float ff // no conversion necessary in Java 1.5+ |
// to String g from Float ff /* 2 decimal places, locale-sensitive */ java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(ff.floatValue()); /* or exponential scientific format, locale-sensitive. */ java.text.DecimalFormat de = new java.text.DecimalFormat("0.000000E00"); g = de.format(ff.floatValue()); /* or best for readbility and maintainability, locale-insensitive. */ g = ff.toString(); |
// to Boolean tt from Float ff tt = (ff.floatValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from Float ff ss = new Short(ff.shortValue()); |
// to Character cc from Float ff cc = new Character((char)ff.intValue()); |
// to Integer ii from Float ff ii = new Integer(ff.intValue()); |
// to Long nn from Float ff nn = new Long(ff.longValue()); |
-------- | // to Double dd from Float ff // no conversion necessary in Java 1.5+ |
Float ff |
| Double dd | // to boolean t from Double dd t = dd.doubleValue() != 0; |
// to /*signed*/ byte b from Double dd b = (byte)dd.intValue(); |
// to /*unsigned*/ byte u from Double dd u = (byte)dd.intValue(); |
// to short s from Double dd s = (short)dd.intValue(); |
// to char c from Double dd c = (char)dd.intValue(); |
// to int i from Double dd i = dd.intValue(); |
// to long n from Double dd n = dd.longValue(); |
// to float f from Double dd f = dd.floatValue(); |
// to double d from Double dd // no conversion necessary in Java 1.5+ |
// to String g from Double dd /* 2 decimal places, locale-sensitive */ java.text.DecimalFormat df2 = new java.text.DecimalFormat("###,##0.00"); g = df2.format(dd.doubleValue()); /* or exponential scientific format, locale sensitive. */ java.text.DecimalFormat de = new java.text.DecimalFormat("0.0000000000E00"); g = de.format(dd.doubleValue()); /* or best for readablity and maintainability, locale-insensitive */ g = dd.toString(); |
// to Boolean tt from Double dd tt = (dd.doubleValue() != 0) ? Boolean.TRUE : Boolean.FALSE; |
// to Short ss from Double dd ss = new Short(dd.shortValue()); |
// to Character cc from Double dd cc = new Character((char)dd.intValue()); |
// to Integer ii from Double dd ii = new Integer(dd.intValue()); |
// to Long nn from Double dd nn = new Long(dd.longValue()); |
// to Float ff from Double dd ff = new Float(dd.floatValue()); |
-------- | Double dd |
| to: boolean t | to: /*signed*/ byte b | to: /*unsigned*/ byte u | to: short s | to: char c | to: int i | to: long n | to: float f | to: double d | to: String g | to: Boolean tt | to: Short ss | to: Character cc | to: Integer ii | to: Long nn | to: Float ff | to: Double dd | ||
Interconverting byte [] and String is tricky because there is always an implied encoding translation. "8859_1" encoding simply chops the high byte off or pads the high byte with 0s, what newbies erroneously imagine happens all the time.
String s = "abc"; // string -> byte[] byte [] b = s.getBytes( "8859_1" /* encoding */ ); // byte[] -> String String t = new String( b , "Cp1252" /* encoding */ );
Interconverting char[] and String is easier because there is no encoding involved.
String s = "abc" ; // string -> char[] char[] ca = s.toCharArray(); // char[] -> String String s = new String( ca );
The radix feature of the toString and parseInt methods lets you handle hexadecimal numbers. Just set the radix to 16.
Strings and StringBuffers can be interconverted.
StringBuffer q; String s; q = new StringBuffer(s); s = new String(q); s = q.toString();
Rounding often surprises because fractions like 0.1 cannot be precisely be represented in IEEE floating point format. Often you find yourself having to add tiny numbers just prior to printing to get the desired effects.
// Rounding to an integer: long n = Math.round(d); double d = Math.rint(d); // Rounding to two decimal places: long n = Math.round(d *100.); /* keep as "pennies" */ double d = Math.rint (d *100.)/100.;
The standard conversions give you no leading blanks or leading zeroes. Here is a code snippet to convert an integer to string padded with leading left zeroes. With an obvious modification it would give you lead blanks.
public static String toLZ( int i, int len ) { // converts integer to left-zero padded string, len chars long. String s = Integer.toString(i); if ( s.length() > len ) return s.substring(0,len); else if ( s.length() < len ) // pad on left with zeros return "000000000000000000000000000".substring(0, len - s.length ()) + s; else return s; } // end toLZ
JDK 1.0.2 has no function like C's printf that lets you control how many positions and decimal places you want in your output. You have to roll your own.
JDK 1.1 has formatting picture classes such as java.util.DateFormat, SimpleDateFormat and DecimalFormat.
Formatting is such a common request, you might attain sainthood if you wrote a formatting class that gives you all the power of printf without the overhead of parsing strings for % produce a string. e.g.
public class Form { static String display (int value , int width , int decimalPlaces , int base , boolean leadZeroes); static String display (int value , int width , int decimalPlaces , int base); static String display (int value , int width , int decimalPlaces); static String display (int value , int width); static String display (int value); static String display (double value , int width , int decimalPlaces); static String display (double value , int width); static String display (double value); }
Java has no built-in methods for reading data of the form: 123 456,-4.
You have to roll your own method. Use java.io.StreamTokenizer or java.util.StringTokenizer, perhaps in combination with readLine to get your data into strings. StreamTokenizer has bells and whistles to deal with parsing source code, including white space, comments and numbers. StringTokenizer just splits the text up based on delimiter characters. Then use the conversion methods in the table above to convert to integers etc. See below for a simplified examples of how you would do this.
// Read space or comma-delimited file of integers // Convert from ASCII to internal binary ... BufferedReader in = new BufferedReader(new FileReader("C:\\temp\\foo.in" ) ); StreamTokenizer st = new StreamTokenizer(in); // process the entire file, of space or comma-delimited ints int found = StreamTokenizer.TT_NUMBER; while ( found != StreamTokenizer.TT_EOF ) { found = st.nextToken (); if ( found == StreamTokenizer.TT_NUMBER ) { int i = (int)st.nval; System.out.println(i); } // end if } // end while
// Read space-delimited file of integers // Convert from ASCII to internal binary ... BufferedReader in = new BufferedReader(new FileReader( "C:\\temp\\foo.in" ), 4096 /* buffsize */ ); String aLine = in.readLine(); StringTokenizer st = new StringTokenizer(aLine); // process one line worth of space-delimited integers while ( st.hasMoreTokens() ) { String s = st.nextToken(); int i = Integer.parseInt(s); System.out.println(i); } // end while
Binary is a compact, machine-friendly, human-unintelligible format. For human readable i/o, Java works with Strings of characters. You separately convert these to and from internal binary format e.g. int. See the conversion Amanuensis for how.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 483. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/convert15.html | J:\mindprod\jgloss\convert15.html | |||