Created
April 15, 2021 15:00
-
-
Save tonyjunkes/0945dd57444ab46e17d4e93d4d5f99cb to your computer and use it in GitHub Desktop.
Choose to round a decimal value that is 3 or more places and return the dollar formatted value.
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> | |
// Because of https://tracker.adobe.com/#/view/CF-4199995, | |
// I've come across a few scenarios where dollarFormat() and numberFormat() will not round up when expected/desired. | |
// Looking at how Java handles BigDecimals and rounding them, it seems my expectations are debatable. | |
// But I need to round up from 3+ decimal places so I expect "62.275" to become "6.28". | |
// The above example works in dollarFormat() but not numberFormat(). The ticket has other failing examples. | |
public string function roundedDollarFormat( | |
required numeric amount, | |
boolean roundUp = true | |
) { | |
var scale = 10 ^ 2; | |
var result = ( arguments.roundUp ) | |
? round( arguments.amount * scale ) / scale | |
: floor( arguments.amount * scale ) / scale; | |
return dollarFormat( result ); | |
} | |
rate = 782.14 + 62.275 - 472.00; | |
// Round up | |
writeDump( roundedDollarFormat( rate, true) ); | |
// Round Down (No round) | |
writeDump( roundedDollarFormat( rate, false ) ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment