Skip to content

Instantly share code, notes, and snippets.

@marcins
Created December 12, 2013 11:31
Show Gist options
  • Save marcins/7926638 to your computer and use it in GitHub Desktop.
Save marcins/7926638 to your computer and use it in GitHub Desktop.
<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