Logo

Programming-Idioms

Calculate the SI (International System of Units) prefix, x, of value a.

For example, 'k', 'M', 'G', 'T', etc.

https://en.wikipedia.org/wiki/Metric_prefix
New implementation

Type ahead, or select one

Explain stuff

To emphasize a name: _x โ†’ x

Please be fair if you are using someone's work

You agree to publish under the CC-BY-SA License

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import static java.lang.Math.log;
import static java.lang.Math.pow;
enum SI {
    k("kilo"),
    M("mega"),
    G("giga"),
    T("tera");
    String s;
    static double y = log(1_000);
    SI(String s) { this.s = s; }
    static SI get(double x) {
        int y = (int) (log(x) / SI.y);
        return values()[y - 1];
    }
}
SI si = SI.get(a);
a = a / pow(1_000, si.ordinal() + 1);
String x = si.name();

This is intended to work where a > 999.