import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors;
public class ConvertNumberToChinese { public static void main(String[] args) { System.out.println(convert(10011001)); System.out.println(convert(100)); System.out.println(convert(101)); System.out.println(convert(1003)); System.out.println(convert(1)); }
private static String convert(int number) { ConversionUtils conversionUtils = new ConversionUtils(); List<UnitValue> unitValues = new ArrayList<>(); List<Integer> sortedKeys = new ArrayList(conversionUtils.unitMapping.keySet()); sortedKeys.sort(Collections.reverseOrder());
for (Integer key: sortedKeys) { if (number == 0) { break; } if (String.valueOf(key).length() != String.valueOf(number).length()) { if (!unitValues.isEmpty() && !unitValues.get(unitValues.size() -1).unitName.equals("零")) { UnitValue unitValue = new UnitValue(); unitValue.unitName = "零"; unitValues.add(unitValue); } continue; } else { UnitValue unitValue = new UnitValue(); unitValue.unitKey = number / key; unitValue.unitName = conversionUtils.unitMapping.get(key); unitValues.add(unitValue); number = number % key; } }
List<String> needReplace = getNeedMergeUnits(conversionUtils);
String ret = ""; for (UnitValue unitValue: unitValues) { ret += unitValue.getValues(); } for (String key : needReplace) { if (ret.lastIndexOf(key) != -1) { String sub = ret.substring(0, ret.lastIndexOf(key)); String remainder = ret.substring(ret.lastIndexOf(key)); ret = sub.replaceAll(key, "") + remainder; } }
return ret; }
private static List<String> getNeedMergeUnits(ConversionUtils conversionUtils) { Map<String, Long> counted = conversionUtils.unitMapping.values() .stream() .map(f -> f.length() == 2 ? String.valueOf(f.toCharArray()[1]) : f) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
List<String> needReplace = counted.keySet().stream().filter(f -> counted.get(f) > 1).collect(Collectors.toList()); return needReplace; }
private static class ConversionUtils {
public Map<Integer, String> numberMapping = new HashMap<>();
public Map<Integer, String> unitMapping = new HashMap<>();
public ConversionUtils() { numberMapping.put(1, "一"); numberMapping.put(2, "二"); numberMapping.put(3, "三"); numberMapping.put(4, "四"); numberMapping.put(5, "五"); numberMapping.put(6, "六"); numberMapping.put(7, "七"); numberMapping.put(8, "八"); numberMapping.put(9, "九");
unitMapping.put(100000000, "亿"); unitMapping.put(10000000, "千万"); unitMapping.put(1000000, "百万"); unitMapping.put(100000, "十万"); unitMapping.put(10000, "万"); unitMapping.put(1000, "千"); unitMapping.put(100, "百"); unitMapping.put(10, "十"); unitMapping.put(1, ""); }
}
private static class UnitValue { private ConversionUtils conversionUtils = new ConversionUtils(); public int unitKey; public String unitName;
public String getValues() { return unitName.equals("零") ? unitName : conversionUtils.numberMapping.get(unitKey) + unitName; } } }
|