Solved

convert javascript print function to USD currency

Posted on 2010-08-18
3
280 Views
Last Modified: 2012-05-10
I am using the code below to display a $ amount on my site based on a shopping cart script...It works fine but prints out as 10.00 or 11.55 or 12.04 I want to convert it to USD such as $12.05 or $11.00..I know I can simply add a $ in front of it, but I may need to use different currency types..Thanks
<SCRIPT language="JavaScript">
Print_total();
</script>

Open in new window

0
Comment
Question by:brad0525
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33464008
Please post the script that actually does the Print_total()
0
 

Author Comment

by:brad0525
ID: 33465168
Here you go..thanks
function Print_total( ) {
var strOutput = ""; //String to be written to page
var strTotal = ""; //Total cost formatted as money
var fTotal = 0;
var iNumberOrdered = 0; //Number of products ordered


iNumberOrdered = GetCookie("NumberOrdered");
if ( iNumberOrdered == null )
iNumberOrdered = 0;


for ( i = 1; i <= iNumberOrdered; i++ ) {

NewOrder = "Order." + i;
database = "";
database = GetCookie(NewOrder);

Token0 = database.indexOf("|", 0);
Token1 = database.indexOf("|", Token0+1);
Token2 = database.indexOf("|", Token1+1);
Token3 = database.indexOf("|", Token2+1);
Token4 = database.indexOf("|", Token3+1);

fields = new Array;
fields[0] = database.substring( 0, Token0 ); // Product ID
fields[1] = database.substring( Token0+1, Token1 ); // Quantity
fields[2] = database.substring( Token1+1, Token2 ); // Price
fields[3] = database.substring( Token2+1, Token3 ); // Product Name/Description
fields[4] = database.substring( Token3+1, Token4 ); // Weight
fields[5] = database.substring( Token4+1, database.length ); //Additional Information

fTotal += (parseInt(fields[1]) * parseFloat(fields[2]) );

}

strTotal = moneyFormat(fTotal);
strOutput+=strTotal;
document.write(strOutput);

}

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 33465292
strOutput+="$"+strTotal;
document.write(strOutput);


or


function Print_total(currencyChar) {

.
.
strOutput+=currencyChar+strTotal;
document.write(strOutput);


and have

<SCRIPT language="JavaScript">
Print_total("$");
</script>



0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question