ColdFusion 太大而不能成为整数

人气:254 发布:2022-10-16 标签: coldfusion integer-division coldfusion-2016

问题描述

我正在尝试将大量数据转换为兆字节.我不要小数

I am trying to convert a large number going in to Megabytes. I don't want decimals

numeric function formatMB(required numeric num) output="false" {
    return arguments.num  1024  1024;
    } 

然后它会抛出一个错误

我该如何解决这个问题?

How do I get around this?

推荐答案

你不能改变 Long 的大小,这是 CF 用于整数的.所以你需要 BigInteger 代替:

You can't change the size of a Long, which is what CF uses for integers. So you'll need to BigInteger instead:

numeric function formatMB(required numeric num) {
    var numberAsBigInteger = createObject("java", "java.math.BigInteger").init(javacast("string", num));
    var mbAsBytes = 1024 ^ 2;
    var mbAsBytesAsBigInteger = createObject("java", "java.math.BigInteger").init(javacast("string", mbAsBytes));
    var numberInMb = numberAsBigInteger.divide(mbAsBytesAsBigInteger);
    return numberInMb.longValue();
}

CLI.writeLn(formatMB(2147483648));

但正如 Leigh 指出的那样……对于您正在做的事情,您最好还是这样做:

But as Leigh points out... for what you're doing, you're probably better off just doing this:

return floor(arguments.num / (1024 * 1024));

688