|
One source for storefront software, order processing utilities, credit card transactions
|
JUST FOR FUN
Click an arrow to visit the application.
ALL THE STUFF THAT'S LEFT HANDED
PLAY OUR SLOT MACHINE, WIN NOTHING
GET RID OF THE DOUBLE CLICK SUBMIT
FIX THAT JAVASCRIPT NUMBER
KEEP TABS ON THE HEALTH OF YOUR COLDFUSION SERVER
CREATE A DYNAMIC BAR CHART
|
CONVERT A JS NUMBER TO 2 DECIMAL CURRENCY VALUE
We like this one; it's from our own shop. That not withstanding, the code
deserves exposure. If you work with JavaScript and currency values, you know
that a ".00" will get dropped and that a division will result in up to 16 digits to the right
of the decimal when displaying a number. Over the years there have been numerous schemes to fix JS numbers. They all vary
in terms of size and complexity. This little Javascript function is the simplest we've ever encountered and
will correct your displayed numbers to two decimals by rounding up or down as required.
|
| | GIVE IT A TRY |
|
|
| | HERE IS THE "FIX" FUNCTION CODE |
function fix(m){
var x = parseFloat(m);
var y = eval(x + .005);
var t = new String(y);
var r = t.indexOf('.');
var fixed = t.substring(0,r+3);
if(fixed.indexOf('.') == -1)fixed += '.00';
return fixed;
}
|
| | HOW IT WORKS |
Call this function anytime you need to be sure of displaying a 2 decimal currency
value. Just pass the value you want to fix with the call. IE: fix(333.2345).
The passed string or number, m, in the function is first converted to a number, x.
An amount .005 is added to the number to insure it has at least 2 decimal palces.
The number is then converted to a string value. The string is then truncated at
two places beyond the period. Finally the value is checked for the 2 decimal plases
and returned as a string. It will print
anywhere as a 2 decimal currency value.
|
Writing code is like wrestling alligators:
The more you thrash around, the more gators join the fray!
|
|
|