java - Why is this code giving me a strange output -
the method below written make string version of number, know there methods this, such string.valueof()
, double.tostring()
, or "" + somenumber
.
private static string numtostring(double i) { string revnumber = ""; boolean isneg = false; if (i == 0) { //catch 0 case return "0"; } if (i < 0) { isneg = true; } = math.abs(i); while (i > 0) { //loop backwards through number, loop //finish, otherwise, not output in 'main()' revnumber += "" + % 10; //get end /= 10; //slice end } string number = ""; //reversed (int k = revnumber.length() - 1; k >= 0; k--) { number += revnumber.substring(k, k + 1); } revnumber = null; //let gc work return isneg ? "-" + number : number; //result expression add "-" //if needed. }
although above method should used ints (32-bit), made accept double (64-bit) argument , passed double argument, without decimal, output results same if pass int method well, or decimals, etc...
test:
public static void main(string[] args) { double test = -134; //passing double arg system.out.println(numtostring(test)); //results }
result: (maximum memory results double?):
-323-e5.1223-e33.1123-e43.1023-e43.1913-e43.1813-e43.1713-e43.1613-e43.1513-e43.1413-e43.1313-e43.1213-e43.1113-e43.1013-e43.1903-e43.1803-e999999999999933.1703-e999999999999933.1603-e999999999999933.1503-e999999999999933.1403-e9899999999999933.1303-e999999999999933.1203-e999999999999933.1103-e8899999999999933.1003-e9899999999999933.1992-e999999999999933.1892-e999999999999933.1792-e999999999999933.1692-e999999999999933.1592-e999999999999933.1492-e999999999999933.1392-e999999999999933.1292-e999999999999933.1192-e999999999999933.1092-e999999999999933.1982-e999999999999933.1882-e1999999999999933.1782-e999999999999933.1682-e999999999999933.1582-e999999999999933.1482-e999999999999933.1382-e999999999999933.1282-e1999999999999933.1182-e1999999999999933.1082-e2999999999999933.1972-e2999999999999933.1872-e3999999999999933.1772-e2999999999999933.1672-e1999999999999933.1572-e2999999999999933.1472-e999999999999933.1372-e999999999999933.1272-e1999999999999933.1172-e2999999999999933.1072-e2999999999999933.1962-e1999999999999933.1862-e999999999999933.1762-e999999999999933.1662-e999999999999933.1562-e999999999999933.1462-e999999999999933.1362-e999999999999933.1262-e999999999999933.1162-e999999999999933.1062-e999999999999933.1952-e999999999999933.1852-e999999999999933.1752-e999999999999933.1652-e999999999999933.1552-e999999999999933.1452-e999999999999933.1352-e999999999999933.1252-e999999999999933.1152-e999999999999933.1052-e9899999999999933.1942-e9899999999999933.1842-e999999999999933.1742-e999999999999933.1642-e999999999999933.1542-e999999999999933.1442-e999999999999933.1342-e999999999999933.1242-e999999999999933.1142-e9899999999999933.1042-e999999999999933.1932-e8899999999999933.1832-e9899999999999933.1732-e8899999999999933.1632-e8899999999999933.1532-e8899999999999933.1432-e999999999999933.1332-e9899999999999933.1232-e8899999999999933.1132-e7899999999999933.1032-e8899999999999933.1922-e8899999999999933.1822-e7899999999999933.1722-e6899999999999933.1622-e5899999999999933.1522-e5899999999999933.1422-e6899999999999933.1322-e6899999999999933.1222-e6899999999999933.1122-e7899999999999933.1022-e7899999999999933.1912-e7899999999999933.1812-e8899999999999933.1712-e8899999999999933.1612-e7899999999999933.1512-e7899999999999933.1412-e6899999999999933.1312-e6899999999999933.1212-e7899999999999933.1112-e7899999999999933.1012-e7899999999999933.1902-e7899999999999933.1802-e6899999999999933.1702-e7899999999999933.1602-e7899999999999933.1502-e8899999999999933.1402-e8899999999999933.1302-e8899999999999933.1202-e8899999999999933.1102-e7899999999999933.1002-e7899999999999933.1991-e9899999999999933.1891-e9899999999999933.1791-e8899999999999933.1691-e7899999999999933.1591-e8899999999999933.1491-e8899999999999933.1391-e999999999999933.1291-e9899999999999933.1191-e9899999999999933.1091-e999999999999933.1981-e999999999999933.1881-e999999999999933.1781-e999999999999933.1681-e999999999999933.1581-e1999999999999933.1481-e999999999999933.1381-e999999999999933.1281-e1999999999999933.1181-e2999999999999933.1081-e999999999999933.1971-e1999999999999933.1871-e999999999999933.1771-e2999999999999933.1671-e3999999999999933.1571-e3999999999999933.1471-e2999999999999933.1371-e2999999999999933.1271-e2999999999999933.1171-e999999999999933.1071-e999999999999933.1961-e999999999999933.1861-e999999999999933.1761-e999999999999933.1661-e999999999999933.1561-e999999999999933.1461-e8899999999999933.1361-e8899999999999933.1261-e8899999999999933.1161-e7899999999999933.1061-e7899999999999933.1951-e7899999999999933.1851-e7899999999999933.1751-e7899999999999933.1651-e7899999999999933.1551-e6899999999999933.1451-e6899999999999933.1351-e6899999999999933.1251-e6899999999999933.1151-e6899999999999933.1051-e7899999999999933.1941-e6899999999999933.1841-e7899999999999933.1741-e7899999999999933.1641-e9899999999999933.1541-e999999999999933.1441-e999999999999933.1341-e999999999999933.1241-e999999999999933.1141-e999999999999933.1041-e999999999999933.1931-e9899999999999933.1831-e999999999999933.1731-e999999999999933.1631-e8899999999999933.1531-e9899999999999933.1431-e9899999999999933.1331-e8899999999999933.1231-e8899999999999933.1131-e8899999999999933.1031-e7899999999999933.1921-e7899999999999933.1821-e7899999999999933.1721-e6899999999999933.1621-e7899999999999933.1521-e7899999999999933.1421-e8899999999999933.1321-e7899999999999933.1221-e7899999999999933.1121-e8899999999999933.1021-e8899999999999933.1911-e9899999999999933.1811-e999999999999933.1711-e999999999999933.1611-e999999999999933.1511-e999999999999933.1411-e1999999999999933.1311-e2999999999999933.1211-e3999999999999933.1111-e2999999999999933.1011-e2999999999999933.1901-e3999999999999933.1801-e2999999999999933.1701-e2999999999999933.1601-e3999999999999933.1501-e2999999999999933.1401-e3999999999999933.1301-e3999999999999933.1201-e4999999999999933.1101-e5999999999999933.1001-e4999999999999933.199-e3999999999999933.189-e3999999999999933.179-e3999999999999933.169-e4999999999999933.159-e4999999999999933.149-e4999999999999933.139-e4999999999999933.129-e4999999999999933.119-e4999999999999933.109-e4999999999999933.198-e4999999999999933.188-e4999999999999933.178-e3999999999999933.168-e3999999999999933.158-e3999999999999933.148-e3999999999999933.138-e3999999999999933.128-e4999999999999933.118-e3999999999999933.108-e3999999999999933.197-e3999999999999933.187-e3999999999999933.177-e4999999999999933.167-e3999999999999933.157-e3999999999999933.147-e3999999999999933.137-e3999999999999933.127-e3999999999999933.117-e4999999999999933.107-e5999999999999933.196-e5999999999999933.186-e5999999999999933.176-e4999999999999933.166-e4999999999999933.156-e4999999999999933.146-e4999999999999933.136-e3999999999999933.126-e3999999999999933.116-e3999999999999933.106-e3999999999999933.195-e2999999999999933.185-e1999999999999933.175-e2999999999999933.165-e2999999999999933.155-e2999999999999933.145-e2999999999999933.135-e3999999999999933.125-e4999999999999933.115-e4999999999999933.105-e2999999999999933.194-e1999999999999933.184-e2999999999999933.174-e2999999999999933.164-e2999999999999933.154-e1999999999999933.144-e1999999999999933.134-e2999999999999933.124-e2999999999999933.114-e2999999999999933.104-e3999999999999933.193-e3999999999999933.183-e3999999999999933.173-e2999999999999933.163-e2999999999999933.153-e999999999999933.143-e2999999999999933.133-e2999999999999933.123-e2999999999999933.113-e3999999999999933.103-e3999999999999933.192-e3999999999999933.182-e3999999999999933.172-e3999999999999933.162-e4999999999999933.152-e4999999999999933.142-e5999999999999933.132-e6999999999999933.122-e7999999999999933.112-e6999999999999933.102-e6999999999999933.191-e7999999999999933.181-e8999999999999933.171-e8999999999999933.161-e8999999999999933.151-e9999999999999933.141-e8999999999999933.131-e8999999999999933.121-e43.111-e43.101-e43.19-e1000000000000043.18-e1000000000000043.17-e43.16-e43.15-e43.14-e43.143100.04310.0431.043.14000000000000004.30.4
this not because of complier. happening because doing
i /= 10; //slice end
so when 13.4 after first run wont give 1.34 give 1.339999999999999999 1.34.
check retain precision double in java more details.
if want reverse number can
private static string numtostring(double i) { string returnstring = new stringbuilder(double.tostring(i)).reverse().tostring(); return i>=0?returnstring:"-"+returnstring.substring(0,returnstring.length()-1); }
Comments
Post a Comment