こんにちは私は誰にでも助けてくれるのですか?2000、1000000などに2k、1m、1gなどを変換する方法
このコードは、2Kおよび1000000 1メートルなど
private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
}
public String format(long value) {
//Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
if (value < 0) return "-" + format(-value);
if (value < 1000) return Long.toString(value); //deal with easy case
Map.Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();
long truncated = value/(divideBy/10); //the number part of the output times 10
boolean hasDecimal = truncated < 100 && (truncated/10d) != (truncated/10);
return hasDecimal ? (truncated/10d) + suffix : (truncated/10) + suffix;
}
に 2000を変換する私は1000000
'k'を' 000'に置き換えますか? 'string.replace(" k "、" 000 ")'のように? –
助けてもらえますか? – Prateek218
言及されていませんが、有効な値は '2k7'でもあります。 – arminb