Created
December 12, 2013 11:31
-
-
Save marcins/7926638 to your computer and use it in GitHub Desktop.
http://cfmlblog.adamcameron.me/2013/12/help-me-with-some-code-if-you-have.html When in doubt, fall back to Java?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
testNumbers = [ | |
"0", "0.0", "0.00", "0.000", "0.001", | |
"0.1", "0.10", "0.100", "0.101", | |
"0.11", "0.110", "0.111" | |
]; | |
expectedNumbers = [ | |
"0", "0", "0", "0", "0", | |
"0.1", "0.1", "0.1", "0.1", | |
"0.11", "0.11", "0.11" | |
]; | |
function formatter(n){ | |
// http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html | |
var decimalFormatter = createObject("java", "java.text.DecimalFormat").init("0.##"); | |
decimalFormatter.setMaximumFractionDigits(2); | |
var formattedN = decimalFormatter.format(javaCast("double", n)); | |
return formattedN; | |
} | |
for (i = 1; i <= arrayLen(testNumbers); i += 1){ | |
result = formatter(testNumbers[i]); | |
expected = javaCast("string", "#expectedNumbers[i]#"); | |
color = expected.equals("#result#") ? "##090" : "##900"; | |
writeOutput("<span style='color:#color#'>#testNumbers[i]#: #result# (Expected: #expectedNumbers[i]#)</span><br>"); | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment