Solved

Proper US Currency Format Script Mod

Posted on 2006-11-30
12
454 Views
Last Modified: 2009-12-16
Hi All

I need this script modified a little. I need it to hold the total amount in the proper US currency format. It needs it to print $50.00 not $50

CODE:
<html>
<head>
<title> Extra Price </title>
<script type='text/javascript'>
  function writeNew( sel, spId, bpId ) {
    var sp  = document.getElementById( spId );
    var bp  = document.getElementById( bpId );
    var so  = sel.options[ sel.selectedIndex ];
    var prices = so.value.split('$' );
    var extra = ship = other = 0.0;
    if ( prices.length > 1 ) extra  = parseFloat( prices[ 1 ] );
    if ( prices.length > 2 ) ship   = parseFloat( prices[ 2 ] );
    if ( prices.length > 3 ) other  = parseFloat( prices[ 3 ] );
    if ( extra == NaN ) { extra = 0; }
    if ( ship  == NaN ) { ship  = 0; }
    if ( other == NaN ) { other = 0; }
    var bpVal = parseFloat( bp.value );
    sp.innerHTML = '$' + ( extra + bpVal );
  }
</script>
</head>
<body>

<form name='Item1' action=''>
  <select onchange='writeNew(this,"text1","bp1")'>
    <option value='Radio 1 [$5.00]'>Radio 1</option>
    <option value='Radio 2 []'>Radio 2</option>
    <option value='Radio 3 [$15.00]'>Radio 3</option>
  </select>

  <input id='bp1' type='hidden' name='BasePrice' value='30.00'></input>
  <span id='text1'>***</span>
</form>

<form name='Item2' action=''>
  <select onchange='writeNew(this,"text2","bp2")'>
    <option value='DVD Player 1 [$5.00] [$10.00]'>DVD Player 1</option>
    <option value='DVD Player 2 [$10.00] [$10.00]'>DVD Player 2</option>
    <option value='DVD Player 3 [$15.00] [$10.00]'>DVD Player 3</option>
  </select>

  <input id='bp2' type='hidden' name='BasePrice' value='40.00'></input>
  <span id='text2'>***</span>
</form>

<form name='Item3' action=''>
  <select onchange='writeNew(this,"text3","bp3")'>
    <option value='Television 1 [$5.00] [$10.00] [$20.00]'>Television 1</option>
    <option value='Television 2 [$10.00] [$10.00] [$20.00]'>Television 2</option>
    <option value='Television 3 [$15.00] [$10.00] [$20.00]'>Television 3</option>
  </select>

  <span id='text3'>***</span>
  <input id='bp3' type='hidden' name='BasePrice' value='50.00'></input>
</form

</body>
</html>
0
Comment
Question by:blnukem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 18044741
function currency(val){
      val = val + '';
      indexPoint = val.indexOf(".");
      if(indexPoint < 0){
            val += '.00';
      } else if(indexPoint == val.length - 2){
            val += '0';
      }
      return val;
}


replace   sp.innerHTML = '$' + ( extra + bpVal );
with        sp.innerHTML = '$' + currency(( extra + bpVal ));
0
 
LVL 6

Expert Comment

by:system_down
ID: 18044771
hi ,

<html>
<head>
<title> Extra Price </title>
<script type='text/javascript'>
  function writeNew( sel, spId, bpId ) {
    var sp  = document.getElementById( spId );
    var bp  = document.getElementById( bpId );
    var so  = sel.options[ sel.selectedIndex ];
    var prices = so.value.split('$' );
    var extra = ship = other = 0.0;
    if ( prices.length > 1 ) extra  = parseFloat( prices[ 1 ] );
    if ( prices.length > 2 ) ship   = parseFloat( prices[ 2 ] );
    if ( prices.length > 3 ) other  = parseFloat( prices[ 3 ] );
    if ( extra == NaN ) { extra = 0; }
    if ( ship  == NaN ) { ship  = 0; }
    if ( other == NaN ) { other = 0; }
    var bpVal = parseFloat( bp.value );
    var total = extra + bpVal;
      sp.innerHTML = '$' + ( total.toFixed(2) );
  }
</script>
</head>
<body>

<form name='Item1' action=''>
  <select onchange='writeNew(this,"text1","bp1")'>
    <option value='Radio 1 [$5.00]'>Radio 1</option>
    <option value='Radio 2 []'>Radio 2</option>
    <option value='Radio 3 [$15.00]'>Radio 3</option>
  </select>

  <input id='bp1' type='hidden' name='BasePrice' value='30.00'></input>
  <span id='text1'>***</span>
</form>

<form name='Item2' action=''>
  <select onchange='writeNew(this,"text2","bp2")'>
    <option value='DVD Player 1 [$5.00] [$10.00]'>DVD Player 1</option>
    <option value='DVD Player 2 [$10.00] [$10.00]'>DVD Player 2</option>
    <option value='DVD Player 3 [$15.00] [$10.00]'>DVD Player 3</option>
  </select>

  <input id='bp2' type='hidden' name='BasePrice' value='40.00'></input>
  <span id='text2'>***</span>
</form>

<form name='Item3' action=''>
  <select onchange='writeNew(this,"text3","bp3")'>
    <option value='Television 1 [$5.00] [$10.00] [$20.00]'>Television 1</option>
    <option value='Television 2 [$10.00] [$10.00] [$20.00]'>Television 2</option>
    <option value='Television 3 [$15.00] [$10.00] [$20.00]'>Television 3</option>
  </select>

  <span id='text3'>***</span>
  <input id='bp3' type='hidden' name='BasePrice' value='50.00'></input>
</form

</body>
</html>

let me know
S_D
0
 
LVL 6

Expert Comment

by:system_down
ID: 18044790

maybe is better to tell you the changes I made:

//new var total as sum
var total = extra + bpVal;
//toFixed is a native function to keep decimals
sp.innerHTML = '$' + ( total.toFixed(2) );

let me know
S_D
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 18044793
or even simpler:

sp.innerHTML = '$' + ( extra + bpVal ).toFixed( 2 );
0
 
LVL 6

Expert Comment

by:system_down
ID: 18044808

I prefer to use another var, is more clear...
but that depend on the programmer...

:D
0
 

Author Comment

by:blnukem
ID: 18044905

Those produce good results whole numbers like 50.00 but how about $50.86 they dont work.
0
 

Author Comment

by:blnukem
ID: 18045145

 Odd it didn't work locally but does on server.
0
 
LVL 6

Expert Comment

by:system_down
ID: 18045275

Sorry but I disagree!!

It is exactly the same answer I posted.
blnukem you could at least share points.

HonorGod is the second time that you pick my points.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18049009
It is never my intension to "pick points".
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18049027
If you note, your update time, and mine are both Date: 11/30/2006 07:50AM EST
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18049113
I apologize for any offense.  It was not, and is not my intension to offend anyone with my replies.  I am only trying to answer questions to the best of my ability, and learn from the questions, and answers presented here.
0
 
LVL 6

Expert Comment

by:system_down
ID: 18051935

I excuse me for the offensive post.

The problem isn't you but lazy question poster that instead of point splitting give all to only one expert... that is frustraiting sometime...

S_D
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

705 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