Link to home
Start Free TrialLog in
Avatar of blnukem
blnukem

asked on

Proper US Currency Format Script Mod

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>
Avatar of enachemc
enachemc
Flag of Afghanistan image

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 ));
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

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
ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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

:D
Avatar of blnukem
blnukem

ASKER


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

ASKER


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

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.
It is never my intension to "pick points".
If you note, your update time, and mine are both Date: 11/30/2006 07:50AM EST
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.

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