[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Proper US Currency Format Script Mod

Posted on 2006-11-30
12
Medium Priority
?
462 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…

656 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