c# - String format numbers to millions, thousands with rounding -
i'm trying format price display, , want display number million (m) or thousands (k) suffix, ever display @ 3 values, rounded down.
i found this question close want, doesn't handle rounding (specifically, rounding down)
likewise, with question have no control on rounding.
sample input/expected output:
1 = 1 10 = 10 100 = 100 1000 = 1k 100000 = 100k 125000 = 125k 125900 = 125k 1000000 = 1m 1250000 = 1.25m 1258000 = 1.25m 10000000 = 10m 10500000 = 10.5m 100000000 = 100m 100100000 = 100m
i ever want display 3 values.
i can't see how can use "," custom specifier , specify rounding.
my initial thinking suggests need use combination of above, math.floor , .tostring() formatting magic, i'm not sure start.
can me out?
thanks in advance.
this should help, combined 1 of formatting techniques in other questions you've linked to.
internal long maxthreesignificantdigits(long x) { int = (int)math.log10(x); = math.max(0, - 2); = (int)math.pow(10, i); return x / * i; }
edit:
ok, how this?
console.writeline(so30180672.formatnumber(1)); console.writeline(so30180672.formatnumber(12)); console.writeline(so30180672.formatnumber(123)); console.writeline(so30180672.formatnumber(1234)); console.writeline(so30180672.formatnumber(12345)); console.writeline(so30180672.formatnumber(123456)); console.writeline(so30180672.formatnumber(1234567)); console.writeline(so30180672.formatnumber(12345678)); console.writeline(so30180672.formatnumber(123456789));
following partially copied here: https://stackoverflow.com/a/23384710/253938
internal class so30180672 { internal static string formatnumber(long num) { num = maxthreesignificantdigits(num); if (num >= 100000000) return (num / 1000000d).tostring("0.#m"); if (num >= 1000000) return (num / 1000000d).tostring("0.##m"); if (num >= 100000) return (num / 1000d).tostring("0k"); if (num >= 100000) return (num / 1000d).tostring("0.#k"); if (num >= 1000) return (num / 1000d).tostring("0.##k"); return num.tostring("#,0"); } internal static long maxthreesignificantdigits(long x) { int = (int)math.log10(x); = math.max(0, - 2); = (int)math.pow(10, i); return x / * i; } }
edit 2 - thank @rhexis
internal class so30180672 { internal static void runtest() { console.writeline(formatnumber(1)); console.writeline(formatnumber(10)); console.writeline(formatnumber(100)); console.writeline(formatnumber(1000)); console.writeline(formatnumber(10000)); console.writeline(formatnumber(100000)); console.writeline(formatnumber(125000)); console.writeline(formatnumber(125900)); console.writeline(formatnumber(1000000)); console.writeline(formatnumber(1250000)); console.writeline(formatnumber(1258000)); console.writeline(formatnumber(10000000)); console.writeline(formatnumber(10500000)); console.writeline(formatnumber(100000000)); console.writeline(formatnumber(100100000)); } private static string formatnumber(long num) { // ensure number has max 3 significant digits (no rounding can happen) long = (long)math.pow(10, (int)math.max(0, math.log10(num) - 2)); num = num / * i; if (num >= 1000000000) return (num / 1000000000d).tostring("0.##") + "b"; if (num >= 1000000) return (num / 1000000d).tostring("0.##") + "m"; if (num >= 1000) return (num / 1000d).tostring("0.##") + "k"; return num.tostring("#,0"); } }
Comments
Post a Comment