Solved

Proper US Currency Format Script Mod

Posted on 2006-11-30
12
447 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

770 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