DreamweaverFAQ.com
Search the site!Search DWfaq.com | Print This PagePrinter Friendly Page
Current: Default | Default: Wimbledon


DreamweaverFAQ.com » Tutorials » JavaScript » Forms And Math Page 2


Forms and Math page 2

Refinements to the "to_currency()" Function

We need to further refine our function, try the example below. Clearly an undesired result. We need to add some zeros.

while (val.length <= 2) {val="0"+val}

Remember that val equals ""+ Math.round(100*val), in the case above val equals ""+Math.round(100*.05), or "5"
The while statement adds leading zeros to "val" as long as "val.length" is less than or equal to 2, so that "5" becomes "005"

<script language="JavaScript">
<!--
function to_currency(val,currency){
if(!val) {
alert("Please provide a value");
return;
}
if(isNaN(val)){
alert(val+" is an invalid input");
return;
}
val=""+Math.round(100*val);
while (val.length <= 2) {
val="0"+val
}

var dec_point=val.length-2;
var first_part=val.substring(0,dec_point);
var second_part=val.substring(dec_point,val.length);
var result=first_part+"."+second_part;
if(!currency){
currency="";
}
result=currency+result;
return result;
}
// -->
</script>

We have created a robust currency converter, but we have not considered the effect of negative numbers. You can try above and you will see the effect of entering a negative number. If we are to limit entries to positive numbers, we could use the "Math.abs(argument)" method, which will return the positive portion of number. Math.abs(-3.14) returns 3.14.

val=""+Math.round(100*Math.abs(val));

<script language="JavaScript">
<!--
function to_currency(val,currency){
if(!val) {
alert("Please provide a value");
return;
}
if(isNaN(val)){
alert(val+" is an invalid input");
return;
}
val=""+Math.round(100*Math.abs(val));
while (val.length <= 2) {
val="0"+val
}
var dec_point=val.length-2;
var first_part=val.substring(0,dec_point);
var second_part=val.substring(dec_point,val.length);
var result=first_part+"."+second_part;
if(!currency){
currency="";
}
result=currency+result;
return result;
}
// -->
</script>

However, we may often have to accommodate negative numbers, and if currency is involved we need it in the following syntax:

- currency value ("-$3.14", for example)

We can simply examine the "val" argument in order to determine if "val" is negative.

var is_negative;
if(val < 0){
is_negative=true
}
else{
is_negative=false
}

We could use a javascript shortcut for these statements:

var is_negative=(val<0)? true:false;

And if it is negative, we'll remove the minus sign from "val":

val=""+Math.round(100*val);
var is_negative=(val<0)? true:false;
if(is_negative){
val=val.substring(1,val.length)
}

The second argument of String.substring() method may be omitted if it points to the end of the string.

if(is_negative){
val=val.substring(1)
}

Tip: You could try using a shorter variable name than is_negative, like is_neg for example. We've kept the long form to help remind you exactly what the variable is used for in this tutorial.

Let's assume we passed negative PI to the function:

val=""+Math.round(100*-3.141592653589793 ); // "-314"
var is_negative=(val<0)? true:false;
if(is_negative){
val="-314".substring(1); // "314"
}

And now we need to manipulate the result:

var sign=is_negative? "-":"";
if(!currency){
currency="";
}
result=sign+currency+result;
return result;

Add these modifications to the scipt as seen below:
<script language="JavaScript">
<!--
function to_currency(val,currency){
if(!val) {
alert("Please provide a value");
return;
}
if(isNaN(val)){
alert(val+" is an invalid input");
return;
}
val=""+Math.round(100*val);
var is_negative=(val<0)? true:false;
if(is_negative){
val=val.substring(1)
}

while (val.length <= 2) {
val="0"+val
}

var dec_point=val.length-2;
var first_part=val.substring(0,dec_point);
var second_part=val.substring(dec_point);
var result=first_part+"."+second_part;
var sign=is_negative? "-":"";
if(!currency){
currency="";
}
result=sign+currency+result;
return result;

}
// -->
</script>

::This page last modified 8/13/2013 at 04:37::

Copyright © 2001-2024 DreamweaverFAQ.com All Rights Reserved.
All brands, trademarks, tutorials, extensions, code, and articles are the property of their respective owners.
A production of Site Drive Inc.
Legal Notice | Privacy Policy | Disclaimer & Notice