Solved

convert javascript print function to USD currency

Posted on 2010-08-18
3
278 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now