@Test public void toCoinsString() { assertEquals("10 Gold", CoinUtils.toCoinsString(10)); assertEquals("10,000 Gold", CoinUtils.toCoinsString(10000)); assertEquals("10 Gold and 1,000 Silver", CoinUtils.toCoinsString(10.1)); assertEquals("1,000 Silver", CoinUtils.toCoinsString(0.1)); assertEquals("1,000 Silver and 12 Bronze", CoinUtils.toCoinsString(0.10000012)); assertEquals("1,000,000 Gold, 1,000 Silver and 12 Bronze", CoinUtils.toCoinsString(1_000_000.10000012)); assertEquals("0", CoinUtils.toCoinsString(0)); }
public static String toCoinsString(double amount) { String format = fullNumberFormatter.format(amount); String[] split = format.split("\\."); int goldCoins = Integer.parseInt(split[0]); int silverCoins = Integer.parseInt(split[1].substring(0, 4)); int bronzeCoins = Integer.parseInt(split[1].substring(4)); if (goldCoins == 0 && silverCoins == 0 && bronzeCoins == 0) { return "0"; } String coins = ""; if (goldCoins > 0) { coins += commaFormatter.format(goldCoins) + " Gold"; } if (silverCoins > 0) { if (goldCoins > 0) { if (bronzeCoins > 0) { coins += ", "; } else { coins += " and "; } } coins += commaFormatter.format(silverCoins) + " Silver"; } if (bronzeCoins > 0) { if (goldCoins > 0 || silverCoins > 0) { coins += " and "; } coins += commaFormatter.format(bronzeCoins) + " Bronze"; } return coins; }
CoinUtils { public static String toCoinsString(double amount) { String format = fullNumberFormatter.format(amount); String[] split = format.split("\\."); int goldCoins = Integer.parseInt(split[0]); int silverCoins = Integer.parseInt(split[1].substring(0, 4)); int bronzeCoins = Integer.parseInt(split[1].substring(4)); if (goldCoins == 0 && silverCoins == 0 && bronzeCoins == 0) { return "0"; } String coins = ""; if (goldCoins > 0) { coins += commaFormatter.format(goldCoins) + " Gold"; } if (silverCoins > 0) { if (goldCoins > 0) { if (bronzeCoins > 0) { coins += ", "; } else { coins += " and "; } } coins += commaFormatter.format(silverCoins) + " Silver"; } if (bronzeCoins > 0) { if (goldCoins > 0 || silverCoins > 0) { coins += " and "; } coins += commaFormatter.format(bronzeCoins) + " Bronze"; } return coins; } }
CoinUtils { public static String toCoinsString(double amount) { String format = fullNumberFormatter.format(amount); String[] split = format.split("\\."); int goldCoins = Integer.parseInt(split[0]); int silverCoins = Integer.parseInt(split[1].substring(0, 4)); int bronzeCoins = Integer.parseInt(split[1].substring(4)); if (goldCoins == 0 && silverCoins == 0 && bronzeCoins == 0) { return "0"; } String coins = ""; if (goldCoins > 0) { coins += commaFormatter.format(goldCoins) + " Gold"; } if (silverCoins > 0) { if (goldCoins > 0) { if (bronzeCoins > 0) { coins += ", "; } else { coins += " and "; } } coins += commaFormatter.format(silverCoins) + " Silver"; } if (bronzeCoins > 0) { if (goldCoins > 0 || silverCoins > 0) { coins += " and "; } coins += commaFormatter.format(bronzeCoins) + " Bronze"; } return coins; }  }
CoinUtils { public static String toCoinsString(double amount) { String format = fullNumberFormatter.format(amount); String[] split = format.split("\\."); int goldCoins = Integer.parseInt(split[0]); int silverCoins = Integer.parseInt(split[1].substring(0, 4)); int bronzeCoins = Integer.parseInt(split[1].substring(4)); if (goldCoins == 0 && silverCoins == 0 && bronzeCoins == 0) { return "0"; } String coins = ""; if (goldCoins > 0) { coins += commaFormatter.format(goldCoins) + " Gold"; } if (silverCoins > 0) { if (goldCoins > 0) { if (bronzeCoins > 0) { coins += ", "; } else { coins += " and "; } } coins += commaFormatter.format(silverCoins) + " Silver"; } if (bronzeCoins > 0) { if (goldCoins > 0 || silverCoins > 0) { coins += " and "; } coins += commaFormatter.format(bronzeCoins) + " Bronze"; } return coins; }  static String toCoinsString(double amount); }
CoinUtils { public static String toCoinsString(double amount) { String format = fullNumberFormatter.format(amount); String[] split = format.split("\\."); int goldCoins = Integer.parseInt(split[0]); int silverCoins = Integer.parseInt(split[1].substring(0, 4)); int bronzeCoins = Integer.parseInt(split[1].substring(4)); if (goldCoins == 0 && silverCoins == 0 && bronzeCoins == 0) { return "0"; } String coins = ""; if (goldCoins > 0) { coins += commaFormatter.format(goldCoins) + " Gold"; } if (silverCoins > 0) { if (goldCoins > 0) { if (bronzeCoins > 0) { coins += ", "; } else { coins += " and "; } } coins += commaFormatter.format(silverCoins) + " Silver"; } if (bronzeCoins > 0) { if (goldCoins > 0 || silverCoins > 0) { coins += " and "; } coins += commaFormatter.format(bronzeCoins) + " Bronze"; } return coins; }  static String toCoinsString(double amount);  }