Solved

Proper US Currency Format Script Mod

Posted on 2006-11-30
12
441 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now