/* * [Grid15.java] * * Summary: generates Java source code to do conversions for JDK 1.5+. * * Copyright: (c) 2005-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * orrect table entries for unsigned conversions that could not be handled purely with autoboxing. */ package com.mindprod.converter; /** * generates Java source code to do conversions for JDK 1.5+. *

* The guts of the conversion process. * Generate sample Java source code for any source/target pair of types. See howregenerate.txt for how to regenerate * matrix files and test for validity. * Future: char[] byte[] String interconversions. add unsigned Byte signed Byte * types *

* * @author Roedy Green, Canadian Mind Products * @version 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * orrect table entries for unsigned conversions that could not be handled purely with autoboxing. * @noinspection PointlessBooleanExpression * @since 1997 */ final class Grid15 { /** * Sample code showing one or more ways to perform each of the 19 x 19 = 361 possible conversions. from=row=0..18 * to=col=0..18. For JDK 1.5+ with autoboxing/unboxing */ static final String[][] how15 = { { /* from boolean */ null, "b = t ? (byte)1 : (byte)0;", "u = t ? (byte)1 : (byte)0;", "s = t ? (short)1 : (short)0;", "// to get '0' and '1'\n" + "c = t ? '1' : '0';\n" + "// or to get Unicode value 0 or 1\n" + "c = t ? (char)1 : (char)0;", "i = t ? 1 : 0;", "n = t ? 1L : 0L;", "f = t ? 1.0f : 0.0f;", "d = t ? 1.0d : 0.0d;", "g = String.valueOf(t);", "tt = t ? Boolean.TRUE : Boolean.FALSE;", "bb = t ? (byte)1 : (byte)0;", "uu = t ? (byte)1 : (byte)0;", "ss = t ? (short)1 : (short)0;", "// to get '0' or '1'\n" + "cc = t ? (char)1 : (char)0;\n" + "// or to get Unicode 0 or 1\n" + "cc = t ? (char)1 : (char)0;\n", "ii = t ? 1 : 0;", "nn = t ? 1L : 0L;", "ff = t ? 1.0f : 0.0f;", "dd = t ? 1.0d : 0.0d;", }, { /* from signed byte */ "t = b!=0;", null, "u = b;", "s = b;", "// 9 -> '9'\n" + "c = (char)(b + '0');\n" + "// or to get Unicode value\n" + "c = (char)b;", "i = b; // sign extends.", "n = b; // sign extends.", "f = b;", "d = b;", "// best for readability\n" + "g = Integer.toString(b);\n" + "// best for maintainability\n" + "g = String.valueOf(b);\n" + "// or\n" + "g = Integer.toString(b, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(b);\n" + "// or\n" + "g = Integer.toOctalString(b);\n" + "// or\n" + "g = Integer.toHexString(b);\n" + "// or kludgy and slow on unoptimised Javas\n" + "g = \"\" + b;", "tt = (b!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = b;", "uu = b;", "ss = (short)b; // sign extends\n", "// 9 -> '9'\n" + "cc = (char)(b + '0');\n" + "// or to get Unicode value\n" + "cc = (char)b;", "ii = (int)b;", "nn = (long)b;", "ff = (float)b; // sign extends.", "dd = (double)b; // sign extends.", }, { /* from unsigned byte */ "t = u!=0;", "b = u;", null, "s = (short)(u & 0xff);", "// 9 -> '9'\n" + "c = (char)((u & 0xff) + '0');\n" + "// or to get Unicode value\n" + "c = (char)(u & 0xff);", "i = u & 0xff; // does not sign extend", "n = u & 0xff; // does not sign extend.", "f = u & 0xff; // does not sign extend.", "d = u & 0xff; // does not sign extend.", "// best for readability\n" + "g = Integer.toString(u & 0xff);\n" + "// best for maintainability\n" + "g = String.valueOf(u & 0xff);\n" + "// or\n" + "g = Integer.toString(u & 0xff, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(u & 0xff);\n" + "// or\n" + "g = Integer.toOctalString(u & 0xff);\n" + "// or\n" + "g = Integer.toHexString(u & 0xff);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + (u & 0xff);", "tt = (u!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = u;", "uu = u;", "ss = (short)(u & 0xff); // does not sign extend.", "// 9 -> '9'\n" + "cc = (char)((u & 0xff) + '0');\n" + "// or to get Unicode value\n" + "cc = (char)(u & 0xff);", "ii = u & 0xff;", "nn =(long)(u & 0xff);", "ff = (float)(u & 0xff); // does not sign extend.", "dd = (double)(u & 0xff); // does not sign extend." }, { /* from short */ "t = s!=0;", "b = (byte)s;", "u = (byte)s;", null, "// 9 -> '9'\n" + "c = (char)(s + '0');\n" + "// or to get Unicode value\n" + "c = (char)s;", "i = s;", "n = s;", "f = s;", "d = s;", "// best for readability\n" + "g = Integer.toString(s);\n" + "// best for maintainability\n" + "g = String.valueOf(s);\n" + "// or\n" + "g = Integer.toString(s, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(s);\n" + "// or\n" + "g = Integer.toOctalString(s);\n" + "// or\n" + "g = Integer.toHexString(s);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + s;", "tt = (s!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = (byte)s;", "uu = (byte)s;", "ss = s;", "// 9 -> '9'\n" + "cc = (char)(s + '0');\n" + "// or to get Unicode value\n" + "cc = (char)s;", "ii = (int)s;", "nn = (long)s;", "ff = (float)s; /* locale-insensitive */", "dd = (double)s; /* locale-insensitive */", }, { /* from char (unsigned) */ "// to convert '0' or '1'\n" + "t = c!='0';\n" + "// to convert Unicode 0 or 1\n" + "t = c!=0;", "// international\n" + "b = (byte)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "b = (byte)(c - '0');\n" + "// or to get Unicode value\n" + "b = (byte)c;", "// international\n" + "u = (byte)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "u = (byte)(c - '0');\n" + "// or to get Unicode value\n" + "u = (byte)c;", "// international\n" + "s = (short)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "s = (short)(c - '0');\n" + "// or to get Unicode value\n" + "s = (short)c;", null, "// international\n" + "i = Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "i = c - '0';\n" + "// or to get Unicode value\n" + "i = c; // does not sign extend.", "// international\n" + "n = Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "n = c - '0';\n" + "// or to get Unicode value\n" + "n = c; // does not sign extend.", "f = c; // does not sign extend.", "d = c; // does not sign extend.", "g = String.valueOf(c);", "// for '0' or '1'\n" + "tt = (c!='0') ? Boolean.TRUE : Boolean.FALSE;\n" + "// or for Unicode 0 or 1\n" + "tt = (c!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = (byte)c;", "uu = (byte)c;", "// international\n" + "ss = (short)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "ss = (short)(c - '0');\n" + "// or to get Unicode value\n" + "ss = (short)c;", "cc = c;", "// international\n" + "ii = Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "ii = c - '0';\n" + "// or to get Unicode value\n" + "ii = (int)c;", "// international,\n" + "nn = (long)Character.digit(c, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "nn = (long)(c - '0');\n" + "// or to get Unicode value\n" + "nn = (long)c;", "ff = (float)c; // does not sign extend.", "dd = (double)c; // does not sign extend." }, { /* from int */ "t = i!=0;", "b = (byte)i;", "u = (byte)(i & 0xff);", "s = (short)i;", "// 9 -> '9'\n" + "c = (char)(i + '0');\n" + "// or to get Unicode value\n" + "c = (char)i;", null, "n = i;", "f = i;\n" + "// to construct a float out of IEEE bits\n" + "f = Float.intBitsToFloat(i);", "d = i;", "// best for readability\n" + "g = Integer.toString(i);\n" + "// best for maintainability\n" + "g = String.valueOf(i);\n" + "// or\n" + "g = Integer.toString(i, 7 /* radix */);\n" + "// or\n" + "g = Integer.toBinaryString(i);\n" + "// or\n" + "g = Integer.toOctalString(i);\n" + "// or\n" + "g = Integer.toHexString(i);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + i;", "tt = (i!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb =(byte)i;", "uu = (byte)(i & 0xff);", "ss = (short)i;", "// 9 -> '9'\n" + "cc = (char)(i + '0');\n" + "// or to get Unicode value\n" + "cc = (char)i;", "ii = i;", "nn = (long)i;", "ff = (float)i;", "dd = (double)i;" }, { /* from long */ "t = n!=0;", "b = (byte)n;", "u = (byte)(n & 0xff);", "s = (short)n;", "// 9 -> '9'\n" + "c = (char)(n + '0');\n" + "// or to get Unicode value\n" + "c = (char)n;", "i = (int)n;", null, "f = n;", "d = n;\n" + "// to construct a double out of IEEE bits\n" + "d = Double.longBitsToDouble (n);", "// best for readability\n" + "g = Long.toString(n);\n" + "// best for maintainability\n" + "g = String.valueOf(n);\n" + "// or\n" + "g = Long.toString(n, 7 /* radix */);\n" + "// or\n" + "g = Long.toBinaryString(n);\n" + "// or\n" + "g = Long.toOctalString(n);\n" + "// or\n" + "g = Long.toHexString(n);\n" + "// or kludgy and possibly slow\n" + "g = \"\" + n;", "tt = (n!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = (byte)n;", "uu = (byte)n;", "ss = (short)n;", "// 9 -> '9'\n" + "cc = (char)(n + '0');\n" + "// or to get Unicode value\n" + "cc = (char)n;", "ii = (int)n;", "nn = n;", "ff = (float)n;", "dd = (double)n;" }, { /* from float */ "t = f!=0;", "b = (byte)f;", "u = (byte)f;", "s = (short)f;", "c = (char)f;", "i = (int)f;\n" + "// or\n" + "i = Math.round(f);\n" + "// or\n" + "i = (int)Math.ceil(f);\n" + "// or\n" + "i = (int)Math.floor(f);\n" + "// to see the IEEE bits inside a float\n" + "i = Float.floatToIntBits(f);", "n = (long)f;\n" + "// or\n" + "n = Math.round(f);\n" + "// or\n" + "n = (long)Math.ceil(f);\n" + "// or\n" + "n = (long)Math.floor(f);", null, "d = f;", "// 2 decimal places, rounded, locale-sensitive.\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(f);\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.000000E00\");\n" + "g = de.format(f);\n" + "// or best for readability, no loss of precision, locale-insensitive\n" + "g = Float.toString(f);\n" + "// or best for maintainability, no loss of precision, locale-insensitive\n" + "g = String.valueOf(f);\n", "tt = (f!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = (byte)f;", "uu = (byte)f;", "ss = (short)f;", "cc = (char)f;", "ii = (int)f;", "nn = (long)f;", "ff = f;", "dd = (double)f;" }, { /* from double */ "t = d!=0;", "b = (byte)d;", "u = (byte)d;", "s = (short)d;", "c = (char)d;", "i = (int)d;\n" + "// or\n" + "i = (int)Math.round(d);\n" + "// or\n" + "i = (int)Math.ceil(d);\n" + "// or\n" + "i = (int)Math.floor(d);", "n = (long)d;\n" + "// or\n" + "n = Math.round(d);\n" + "// or\n" + "n = (long)Math.ceil(d);\n" + "// or\n" + "n = (long)Math.floor(d);\n" + "// to see the IEEE bits inside a double\n" + "n = Double.doubleToLongBits(d);", "f = (float)d;", null, "// 2 decimal places, rounded, locale-sensitive.\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(d);\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.0000000000E00\");\n" + "g = de.format(d);\n" + "// or best for readability, no loss of precision, locale-insensitive\n" + "g = Double.toString(d);\n" + "// or best for maintainability, no loss of precision, locale-insensitive\n" + "g = String.valueOf(d);", "tt = (d!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = (byte)d;", "uu = (byte)d;", "ss = (short)d;", "cc = (char)d;", "ii = (int)d;", "nn = (long)d;", "ff = (float)d;", "dd = d;", }, { /* from String */ "t = Boolean.valueOf( g.trim() );\n" + "// or\n" + "t = g.trim().equalsIgnoreCase(\"true\");", "try {\n" + "b = (byte)Integer.parseInt(g.trim());\n" + "// or\n" + "b = (byte)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "u = (byte)Integer.parseInt(g.trim());\n" + "// or\n" + "u = (byte)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "s = (short)Integer.parseInt(g.trim());\n" + "// or\n" + "s = (short)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// \"9\" -> '9'\n" + "c = g.charAt(0 /* position */);\n" + "// or to get Unicode value\n" + "c = (char)Integer.parseInt(g.trim());\n" + "// or to get Unicode hex value\n" + "c = (char)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "i = Integer.parseInt(g.trim());\n" + "// or\n" + "i = Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "n = Long.parseLong(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// locale-insensitive\n" + "f = Float.parseFloat(g.trim());\n" + "// locale-insensitive\n" + "f = Float.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// locale-insensitive\n" + "d = Double.parseDouble(g.trim()); \n" + "} catch (NumberFormatException e){/* ... */}", null, "tt = Boolean.valueOf(g.trim());", "try {\n" + "bb = Byte.parseByte(g.trim());\n" + "// or\n" + "bb = Byte.parseByte(g.trim(), 16 /* radix */);\n" + "// or\n" + "bb = (byte)g.charAt(0 /* position */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "uu = Byte.parseByte(g.trim());\n" + "// or\n" + "uu = Byte.parseByte(g.trim(), 16 /* radix */);\n" + "// or\n" + "uu = (byte)g.charAt(0 /* position */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "ss = Short.parseShort(g.trim());\n" + "// or\n" + "ss = Short.parseShort(g.trim(), 16 /* radix */);\n" + "// or\n" + "ss = (short)g.charAt(0 /* position */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// \"9\" -> '9'\n" + "cc = g.charAt(0 /* position */);\n" + "// or to get Unicode value\n" + "cc = (char)Integer.parseInt(g.trim());\n" + "// or to get Unicode hex value\n" + "cc = (char)Integer.parseInt(g.trim(), 16 /* radix */);\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "ii = Integer.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "nn = Long.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// locale-insensitive.\n" + "ff = Float.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", "try {\n" + "// locale-insensitive\n" + "dd = Double.valueOf(g.trim());\n" + "} catch (NumberFormatException e){/* ... */}", }, { /* from Boolean */ "t = tt;", "b = tt ? (byte)1 : (byte)0;", "u = tt ? (byte)1 : (byte)0;", "s = tt ? (short)1 : (short)0;", "// to get '0' and '1'\n" + "c = tt ? '1' : '0';\n" + "// or to get Unicode 0 and 1\n" + "c = tt ? (char)1 : (char)0;", "i = tt ? 1 : 0;", "n = tt ? 1L : 0L;", "f = tt ? 1.0f : 0.0f;", "d = tt ? 1.0d : 0.0d;", "g = tt.toString();", null, "bb = tt ? (byte)1 : (byte)0;", "uu = tt ? (byte)1 : (byte)0;", "ss = tt ? (short)1 : (short)0;", "// to get '0' and '1'\n" + "cc = tt ? '1' : '0';\n" + "// or to get Unicode 0 or 1\n" + "cc = tt ? (char)1 : (char)0;", "ii = tt ? 1 : 0;", "nn = tt ? 1L : 0L;", "ff = tt ? 1.0f : 0.0f;", "dd = tt ? 1.0d : 0.0d;" }, { /* from signed Byte */ "t = bb!=0;", "b = bb;", "u = bb;", "s = bb.shortValue();", "// 9 -> '9'\n" + "c = (char)(bb.intValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)bb.intValue();", "i = bb.intValue(); ", "n = bb.longValue();", "f = bb.floatValue();", "d = bb.doubleValue();", "g = bb.toString();", "tt = (bb!='0') ? Boolean.TRUE : Boolean.FALSE;", null, "uu = bb;", "ss = bb.shortValue();", "// 9 -> '9'\n" + "cc = (char)(bb + '0');\n" + "// or to get Unicode value\n" + "cc = (char)bb.intValue();", "ii = bb.intValue();", "nn = bb.longValue();", "ff = bb.floatValue();", "dd = bb.doubleValue();", }, { /* from unsigned Byte */ "t = uu!=0;", "b = uu;", "u = uu;", "s = (short)(uu.intValue() & 0xff);", "// 9 -> '9'\n" + "c = (char)((uu.intValue() + '0') & 0xff);\n" + "// or to get Unicode value\n" + "c = (char)(uu.intValue() & 0xff);", "i = uu.intValue() & 0xff;", "n = uu.longValue() & 0xff;", "f = (float)(uu.intValue() & 0xff);", "d = (double)(uu.intValue() & 0xff);", "g = uu.toString();", "tt = (uu!='0') ? Boolean.TRUE : Boolean.FALSE;", "bb = uu;", null, "ss = (short)(uu & 0xff);", "// 9 -> '9'\n" + "cc = (char)((uu.intValue() + '0') & 0xff);\n" + "// or to get Unicode value\n" + "cc = (char)(uu.intValue() & 0xff);", "ii = uu.intValue() & 0xff;", "nn = uu.longValue() & 0xff;", "ff = (float)(uu.intValue() & 0xff);", "dd = (double)(uu.intValue() & 0xff);", }, { /* from Short */ "t = ss!=0;", "b = ss.byteValue();", "u = ss.byteValue();", "s = ss;", "// 9 -> '9'\n" + "c = (char)(ss.intValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)ss.intValue();", "i = ss.intValue(); ", "n = ss.longValue();", "f = ss.floatValue();", "d = ss.doubleValue();", "g = ss.toString();", "tt = ss!=0 ? Boolean.TRUE : Boolean.FALSE;", "bb = ss.byteValue();", "uu = ss.byteValue();", null, "// 9 -> '9'\n" + "cc = (char)(ss.intValue() + '0');\n" + "// or to get Unicode value\n" + "cc = (char)ss.intValue();", "ii = ss.intValue();", "nn = ss.longValue();", "ff = ss.floatValue();", "dd = ss.doubleValue();", }, { /* from Character */ "// to convert '0' or '1'\n" + "t = cc!='0';\n" + "// to convert Unicode 0 or 1\n" + "t = cc!=0;", "// international\n" + "b = (byte)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "b = (byte)(cc - '0');\n" + "// or to get Unicode value\n" + "b = (byte)cc.charValue();", "// international\n" + "u = (byte)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "u = (byte)(cc - '0');\n" + "// or to get Unicode value\n" + "u = (byte)cc.charValue();", "// international\n" + "s = (short)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "s = (short)(cc - '0');\n" + "// or to get Unicode value\n" + "s = (short)cc.charValue();", "c = cc;", "// international\n" + "i = Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "i = cc - '0';\n" + "// or to get Unicode value\n" + "i = cc; // does not sign extend.", "// international\n" + "n = Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "n = cc - '0';\n" + "// or to get Unicode value\n" + "n = cc; // does not sign extend.", "f = cc; // does not sign extend.", "d = cc; // does not sign extend.", "g = cc.toString();", "// for '0' or '1'\n" + "tt = cc!='0' ? Boolean.TRUE : Boolean.FALSE;\n" + "// or for Unicode 0 or 1\n" + "tt = cc!=0 ? Boolean.TRUE : Boolean.FALSE;", "// international\n" + "bb =(byte)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "bb = (byte)(cc - '0');\n" + "// or to get Unicode value\n" + "bb = (byte)cc.charValue();", "// international\n" + "uu = (byte)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "uu = (byte)(cc - '0');\n" + "// or to get Unicode value\n" + "uu = (byte)cc.charValue();", "// international\n" + "ss = (short)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "ss = (short)(cc - '0');\n" + "// or to get Unicode value\n" + "ss = (short)cc.charValue();", null, "// international,\n" + "ii = Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "ii = cc - '0';\n" + "// or to get Unicode value \n" + "ii = (int)cc;", "// international\n" + "nn = (long)Character.digit(cc, 10 /* radix */);\n" + "// fastest '9' -> 9\n" + "nn = (long)(cc - '0');\n" + "// or to get Unicode value\n" + "nn = (long)cc;", "ff = (float)cc; // does not sign extend.", "dd = (double)cc; // does not sign extend." }, { /* from Integer */ "t = ii!=0;", "b = (byte)ii.intValue();", "u = (byte)ii.intValue();", "s = (short)ii.intValue();", "c = (char)ii.intValue();", "i = ii;", "n = ii.longValue();", "f = ii.floatValue();", "d = ii.doubleValue();", "g = ii.toString();", "tt = (ii!= 0) ? Boolean.TRUE : Boolean.FALSE;", "bb = ii.byteValue();", "uu = ii.byteValue();", "ss = ii.shortValue();", "// 9 -> '9'\n" + "cc = (char)(ii + '0');\n" + "// or to get Unicode value\n" + "cc = (char)ii.intValue();", null, "nn = ii.longValue();", "ff = ii.floatValue();", "dd = ii.doubleValue();", }, { /* from Long */ "t = nn!=0;", "b = (byte)nn.intValue();", "u = (byte)nn.intValue();", "s = (short)nn.intValue();", "// 9 -> '9'\n" + "c = (char)(nn.intValue() + '0');\n" + "// or to get Unicode value\n" + "c = (char)nn.intValue();", "i = nn.intValue();", "n = nn;", "f = nn.floatValue();", "d = nn.doubleValue();", "g = nn.toString();", "tt = (nn!= 0) ? Boolean.TRUE : Boolean.FALSE;", "bb = nn.byteValue();", "uu = nn.byteValue();", "ss = nn.shortValue();", "// 9 -> '9'\n" + "cc = (char)(nn.intValue() + '0');\n" + "// or to get Unicode value\n" + "cc = (char)nn.intValue();", "ii = nn.intValue();", null, "ff = nn.floatValue();", "dd = nn.doubleValue();", }, { /* from Float */ "t = ff!=0;", "b = (byte)ff.intValue();", "u = (byte)ff.intValue();", "s = (short)ff.intValue();", "c = (char)ff.intValue();", "i = ff.intValue();", "n = ff.longValue();", "f = ff;", "d = ff.doubleValue();", "// 2 decimal places, rounded, locale-sensitive\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(ff);\n" + "// or exponential scientific format, locale-sensitive.\n" + "java.text.DecimalFormat de\n" + " = new java.text.DecimalFormat(\"0.000000E00\");\n" + "g = de.format(ff); \n" + "// or best for readability and maintainability, locale-insensitive.\n" + "g = ff.toString();", "tt = (ff!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = ff.byteValue();", "uu = ff.byteValue();", "ss = ff.shortValue();", "cc = (char)ff.intValue();", "ii = ff.intValue();", "nn = ff.longValue();", null, "dd = ff.doubleValue();", }, { /* from Double */ "t = dd!=0;", "b = (byte)dd.intValue();", "u = (byte)dd.intValue();", "s = (short)dd.intValue();", "c = (char)dd.intValue();", "i = dd.intValue();", "n = dd.longValue();", "f = dd.floatValue();", "d = dd;", "// 2 decimal places, rounded, locale-sensitive\n" + "java.text.DecimalFormat df2\n" + " = new java.text.DecimalFormat(\"###,##0.00\");\n" + "g = df2.format(dd);\n" + "// or exponential scientific format, locale sensitive.\n" + "java.text.DecimalFormat df\n" + " = new java.text.DecimalFormat(\"0.0000000000E00\");\n" + "g = df.format(dd); \n" + "// or best for readability and maintainability, locale-insensitive\n" + "g = dd.toString();", "tt = (dd!=0) ? Boolean.TRUE : Boolean.FALSE;", "bb = dd.byteValue();", "uu = dd.byteValue();", "ss = dd.shortValue();", "cc = (char)dd.intValue();", "ii = dd.intValue();", "nn = dd.longValue();", "ff = dd.floatValue();", null, } }; /** * Hide the constructor. ConvertGrid is purely static. */ private Grid15() { /* statics only */ } } // end Grid15