ColdFusion is too big to be whole

I am trying to convert a large number to megabytes. I do not want decimal places

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

Then it gives an error

enter image description here

How do I get around this?

+6
source share
2 answers

You cannot resize the Long that CF uses for integers. Therefore you need BigInteger :

 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)); 

But since Lee points out ... for what you are doing, you are probably best off doing this:

 return floor(arguments.num / (1024 * 1024)); 
+7
source

Long size used by CF for integers

A small correction for those who do not read comments. CF uses 32-bit signed integers , not Long (which has a much larger capacity). Since the error message indicates, the size limit here is the Integer capacity:

  • Integer.MAX_VALUE = 2147483647
  • Long.MAX_VALUE = 9223372036854775807

It is worth noting that although CF is relatively featureless, some Math and Date functions also have the same limitations. For example, although DateAdd technically supports milliseconds if you try to use a very large number:

 // getTime() - returns number of milliseconds since January 1, 1970 currentDate = dateAdd("l", now().getTime(), createDate(1970,1,1)); 

... it will fail with the same error, because the parameter "number" must be an integer. Therefore, note if the documentation mentions "Integer". It does not just mean "number" or "numerical" ...

+1
source

All Articles