在 Java 中自动将测量值格式化为工程单位

发布于 2024-10-18 06:03:15 字数 541 浏览 5 评论 0原文

我正在尝试找到一种方法来自动将测量值和单位格式化为 工程符号。这是科学记数法的一个特例,指数始终是三的倍数,但使用千、兆、毫、微前缀表示。

这类似于这篇文章 除了它应该处理整个 SI 单位和前缀范围。

例如,我正在寻找一个可以格式化数量的库,以便: 12345.6789 Hz 将被格式化为 12 kHz 或 12.346 kHz 或 12.3456789 kHz 1234567.89 J 将被格式化为 1 MJ 或 1.23 MJ 或 1.2345 MJ 等等。

JSR-275 / JScience 可以很好地处理单位测量,但我还没有找到可以根据测量大小自动计算出最合适的缩放前缀的东西。

干杯, 萨姆.

I'm trying to find a way to automatically format a measurement and unit into a String in engineering notation. This is a special case of scientific notation, in that the exponent is always a multiple of three, but is denoted using kilo, mega, milli, micro prefixes.

This would be similar to this post except it should handle the whole range of SI units and prefixes.

For example, I'm after a library that will format quantities such that:
12345.6789 Hz would be formatted as 12 kHz or 12.346 kHz or 12.3456789 kHz
1234567.89 J would be formatted as 1 MJ or 1.23 MJ or 1.2345 MJ
And so on.

JSR-275 / JScience handle the unit measurement ok, but I'm yet to find something that will work out the most appropriate scaling prefix automatically based on the magnitude of the measurement.

Cheers,
Sam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-10-25 06:03:15
import java.util.*;
class Measurement {
    public static final Map<Integer,String> prefixes;
    static {
        Map<Integer,String> tempPrefixes = new HashMap<Integer,String>();
        tempPrefixes.put(0,"");
        tempPrefixes.put(3,"k");
        tempPrefixes.put(6,"M");
        tempPrefixes.put(9,"G");
        tempPrefixes.put(12,"T");
        tempPrefixes.put(-3,"m");
        tempPrefixes.put(-6,"u");
        prefixes = Collections.unmodifiableMap(tempPrefixes);
    }

    String type;
    double value;

    public Measurement(double value, String type) {
        this.value = value;
        this.type = type;
    }

    public String toString() {
        double tval = value;
        int order = 0;
        while(tval > 1000.0) {
            tval /= 1000.0;
            order += 3;
        }
        while(tval < 1.0) {
            tval *= 1000.0;
            order -= 3;
        }
        return tval + prefixes.get(order) + type;
    }

    public static void main(String[] args) {
        Measurement dist = new Measurement(1337,"m"); // should be 1.337Km
        Measurement freq = new Measurement(12345678,"hz"); // should be 12.3Mhz
        Measurement tiny = new Measurement(0.00034,"m"); // should be 0.34mm

        System.out.println(dist);
        System.out.println(freq);
        System.out.println(tiny);

    }

}
import java.util.*;
class Measurement {
    public static final Map<Integer,String> prefixes;
    static {
        Map<Integer,String> tempPrefixes = new HashMap<Integer,String>();
        tempPrefixes.put(0,"");
        tempPrefixes.put(3,"k");
        tempPrefixes.put(6,"M");
        tempPrefixes.put(9,"G");
        tempPrefixes.put(12,"T");
        tempPrefixes.put(-3,"m");
        tempPrefixes.put(-6,"u");
        prefixes = Collections.unmodifiableMap(tempPrefixes);
    }

    String type;
    double value;

    public Measurement(double value, String type) {
        this.value = value;
        this.type = type;
    }

    public String toString() {
        double tval = value;
        int order = 0;
        while(tval > 1000.0) {
            tval /= 1000.0;
            order += 3;
        }
        while(tval < 1.0) {
            tval *= 1000.0;
            order -= 3;
        }
        return tval + prefixes.get(order) + type;
    }

    public static void main(String[] args) {
        Measurement dist = new Measurement(1337,"m"); // should be 1.337Km
        Measurement freq = new Measurement(12345678,"hz"); // should be 12.3Mhz
        Measurement tiny = new Measurement(0.00034,"m"); // should be 0.34mm

        System.out.println(dist);
        System.out.println(freq);
        System.out.println(tiny);

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文