[JS] Trying to work out calculating cents

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

I have just started writing something to help out in the office with quotes and so starting with the basics, i have ran into 2 problems to work on so far.

1) value is not calculating cents
Code: [ Select ]
<html>
<head>
 <title>Get Quote v0.1</title>
    <script type="text/javascript">
function price() {

  var profile = document.priceCalc.profile32;
    var mtr = document.priceCalc.meter32;
  var size = document.priceCalc.size32;
  var price = parseInt(mtr.value) * parseInt(size.value);
    document.getElementById("prices").value = price.toFixed(2);
}
    </script>
</head>


<body>
<form name="priceCalc" action="">
    <div align="center">
        Meters <input type="text" name="meter32"><br>
    </div>
  <div align="center">
    <select name="profile32" onchange="price();">
      <option value="1">Bolton</option>
      <option value="2">Malvern</option>
            <option value="3">Norley</option>
    </select>
    
    <div align="center">
            <select name="size32" onchange="price();">
                <option value="112.15">125x32</option>
                <option value="100.55">117x32</option>
                <option value="112.15">140x32</option>
                <option value="209.45">240x32</option>
            </select>
        </div>
    <br>
    <input type="text" id="prices">
    <input type="button" value="Get Quote!" onclick="price();"><br>
  </div>
</form>
</body>
</html>
  1. <html>
  2. <head>
  3.  <title>Get Quote v0.1</title>
  4.     <script type="text/javascript">
  5. function price() {
  6.   var profile = document.priceCalc.profile32;
  7.     var mtr = document.priceCalc.meter32;
  8.   var size = document.priceCalc.size32;
  9.   var price = parseInt(mtr.value) * parseInt(size.value);
  10.     document.getElementById("prices").value = price.toFixed(2);
  11. }
  12.     </script>
  13. </head>
  14. <body>
  15. <form name="priceCalc" action="">
  16.     <div align="center">
  17.         Meters <input type="text" name="meter32"><br>
  18.     </div>
  19.   <div align="center">
  20.     <select name="profile32" onchange="price();">
  21.       <option value="1">Bolton</option>
  22.       <option value="2">Malvern</option>
  23.             <option value="3">Norley</option>
  24.     </select>
  25.     
  26.     <div align="center">
  27.             <select name="size32" onchange="price();">
  28.                 <option value="112.15">125x32</option>
  29.                 <option value="100.55">117x32</option>
  30.                 <option value="112.15">140x32</option>
  31.                 <option value="209.45">240x32</option>
  32.             </select>
  33.         </div>
  34.     <br>
  35.     <input type="text" id="prices">
  36.     <input type="button" value="Get Quote!" onclick="price();"><br>
  37.   </div>
  38. </form>
  39. </body>
  40. </html>


2) Trying to find a way to have a multi level version as i got 4 sizes 12,18,25,32 and then 50 profiles each size and then 3-5 sizes on each and so exploring options on what is easy to use for the sales team.
- Profile 1
-- Size 1
-- Size 2
- Profile 2
-- Size 1
-- Size 2
-- Size 3

I have not written this yet and just about to start that i need a meter rate now each profile is 5.4 meters long and so a customer would want 20 meters and so working out a way to calculate the meters with a profile and size variables and then alter prices with half and full pack rates. (Should be fun lol)

-- Update
o.k i found the fixedto variable and so now i got a .00 but all 0's and not .15 for example per meter and so trying to pass through decimal data now.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9090
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Not sure if I am understanding correctly, but you are using parseInt which will take whatever number/string you have and just return the integer portion which means if a number is 4.322, then parseInt is just going to return 4. That would explain why everything would be ending in .00 as there are no decimals being multiplied here.
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1244
  • Loc: Sydney

Post 3+ Months Ago

Bigwebmaster wrote:
Not sure if I am understanding correctly, but you are using parseInt which will take whatever number/string you have and just return the integer portion which means if a number is 4.322, then parseInt is just going to return 4. That would explain why everything would be ending in .00 as there are no decimals being multiplied here.


aaahhh that explains a lot, let me just alter that and should be sweet to move onto the next problem.
  • Kurthead+1
  • Graduate
  • Graduate
  • Kurthead+1
  • Posts: 131

Post 80 days ago

Bigwebmaster wrote:
Not sure if I am understanding correctly, but you are using parseInt which will take whatever number/string you have and just return the integer portion which means if a number is 4.322, then parseInt is just going to return 4. That would explain why everything would be ending in .00 as there are no decimals being multiplied here.


Haha good catch.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 79 days ago

parseInt() returns everything before the decimal parseFloat() includes every thing after the decimal
HTML Code: [ Select ]
    <html>
    <head>
     <title>Get Quote v0.1</title>
        <script type="text/javascript">
    function price() {
      var profile = document.priceCalc.profile32;
        var mtr = document.priceCalc.meter32;
      var size = document.priceCalc.size32;
      var price = parseFloat(mtr.value) * parseFloat(size.value);
        document.getElementById("prices").value = price.toFixed(2);
    }
        </script>
    </head>
    <body>
    <form name="priceCalc" action="">
        <div align="center">
            Meters <input type="text" name="meter32"><br>
        </div>
      <div align="center">
        <select name="profile32" onchange="price();">
          <option value="1">Bolton</option>
          <option value="2">Malvern</option>
                <option value="3">Norley</option>
        </select>
       
        <div align="center">
                <select name="size32" onchange="price();">
                    <option value="112.15">125x32</option>
                    <option value="100.55">117x32</option>
                    <option value="112.15">140x32</option>
                    <option value="209.45">240x32</option>
                </select>
            </div>
        <br>
        <input type="text" id="prices">
        <input type="button" value="Get Quote!" onclick="price();"><br>
      </div>
    </form>
    </body>
    </html>
 
  1.     <html>
  2.     <head>
  3.      <title>Get Quote v0.1</title>
  4.         <script type="text/javascript">
  5.     function price() {
  6.       var profile = document.priceCalc.profile32;
  7.         var mtr = document.priceCalc.meter32;
  8.       var size = document.priceCalc.size32;
  9.       var price = parseFloat(mtr.value) * parseFloat(size.value);
  10.         document.getElementById("prices").value = price.toFixed(2);
  11.     }
  12.         </script>
  13.     </head>
  14.     <body>
  15.     <form name="priceCalc" action="">
  16.         <div align="center">
  17.             Meters <input type="text" name="meter32"><br>
  18.         </div>
  19.       <div align="center">
  20.         <select name="profile32" onchange="price();">
  21.           <option value="1">Bolton</option>
  22.           <option value="2">Malvern</option>
  23.                 <option value="3">Norley</option>
  24.         </select>
  25.        
  26.         <div align="center">
  27.                 <select name="size32" onchange="price();">
  28.                     <option value="112.15">125x32</option>
  29.                     <option value="100.55">117x32</option>
  30.                     <option value="112.15">140x32</option>
  31.                     <option value="209.45">240x32</option>
  32.                 </select>
  33.             </div>
  34.         <br>
  35.         <input type="text" id="prices">
  36.         <input type="button" value="Get Quote!" onclick="price();"><br>
  37.       </div>
  38.     </form>
  39.     </body>
  40.     </html>
  41.  
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 79 days ago

I just saw the date of the original post. not sure if you have come across that solution yet or not.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 102 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.