Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1318
  • Last Modified:

Divide two numbers with Javascript

Hello all

I need to divide two numbers using Javascript.

So far, so good. The problem is that the first number comes from an external .js file on another page and the second number has a decimal point.

Below is the page that I want to put the script on. I want to divide the figure that would appear in 'pants[3]' by 1.56. The result would then be shown on a different part of the page.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Property Details</title>

<link rel="stylesheet" href="escapes.css" type="text/css">
<script language="JavaScript" src="escapes.js"></script>

<style type="text/css">
      a {font-family:Geneva, Arial, Helvetica, sans-serif; font-size:75%; color:#A52A2A; font-weight:bold; letter-spacing:1.2px; text-decoration:none;}

      a:hover {font-family:Geneva, Arial, Helvetica, sans-serif; font-size:75%; color:#986830; font-weight:bold; letter-spacing:1.2px;text-decoration:none;}

      .divScroll {scrollbar-base-color:#FFE8C0;scrollbar-arrow-color:#A52A2A;scrollbar-shadow-color:#FFCC68}
</style>

<script language="JavaScript" type="text/javascript">
/* read_pants() Script by ren_b. Used with loadPicture() script (see referrer page). ren_b can be found at Experts Exchange. All the other nonsense by me.

Original script by ren_b: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21067845.html

As always, use these scripts by all means, but keep the credits in. Those who did the work deserve it. */

var pants = new Array();
function read_pants() {
pants = document.location.toString().split('?')[1].split('&');
for(i in pants)
pants[i] = unescape(pants[i]);

document.title=pants[0]+", "+pants[1]+" - Ref: "+pants[2];

document.write("<table align='center' width='95%' cellspacing='0' cellpadding='0' border='0'>");
document.write("<td align='right' valign='top' width='100%'>");
      document.write("<table align='center' width='100%' cellspacing='0' cellpadding='0' border='0'>");
            document.write("<td width='66%' height='20' class='headline' align='left'>"+pants[0]+", "+pants[1]+"</td>");
            document.write("<td width='33%' height='20' class='headline' align='right'>Ref: "+pants[2]+"</td>");
            document.write("<tr>");
            document.write("<td colspan='2' height='20' class='standard'>"+pants[3]+" - "+pants[4]+" - built in "+ pants[8] +"</td>");
            document.write("<tr>");
            document.write("<td colspan='2' height='20' class='standard'>"+ pants[7] +" sq. feet - "+ pants[5] +" bedroom(s), "+ pants[6] +" bathroom(s)</td>");
            document.write("<tr>");
            document.write("<td colspan='2' class='standard' style='padding-top:5;padding-bottom:10'>"+pants[9]+"</td>");
            document.write("<tr>");
            document.write("<td colspan='2' class='standard' style='padding-bottom:10'>"+pants[10]+"</td>");
      document.write("</table>");
document.write("</td>");
document.write("</table>");

}


</script>

</head>

<body id="bummer" style="margin:0;">

<table align="center" border="0">

      <td width="100%" align="center">
            <div style="height:435;overflow:auto;" class="divScroll"><script>read_pants();</script></div>
      </td>
      <tr>
      <td width="100%" height="25" align="right">
            <a href="javascript:void(0)" onclick="self.close()" onmouseover="opener.status='Close this window';return true" onfocus="this.blur()" style="padding-right:5px;border-top:solid thin #FFCC68;width:200px;height:25px;padding-top:5px">Close this window</a>
      </td>

</table>

</body>
</html>

Hope this makes sense.

Regards

Pantyboy
0
Martin Cotterill
Asked:
Martin Cotterill
1 Solution
 
archrajanCommented:
var d = Math.round(a.value*100)/100
var e = 1.56
var c = d / e
0
 
Martin CotterillDabblerAuthor Commented:
Hi archrajan

Thanks for the reply.

This much I can do. My problem is that I need to use whatever the amount is in 'pants[3]' and I can't figure out how to use the variable, 'pants[3]', in the coding.

Regards

Pantyboy
0
 
dakydCommented:
Unless I'm misunderstanding you, something like this should do the trick:

var d = parseFloat(pants[3]);
var e = 1.56;
var answer = d / e;

pants[3] has a string, not a number.  That's probably why what you had didn't work.  You can convert it to a number first by using parseFloat().  Then you can do any math operations you want.  Hope that helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Martin CotterillDabblerAuthor Commented:
Hi dakyd

That just about does it, but my answer comes out as '233.97435897435898' and if I use 'Math.round(d / e)' it comes out as 234. I need it to show as 233,974. Any ideas?

Regards

Pantyboy
0
 
dakydCommented:
You can use toFixed() to cut off the number after a given number of decimal places:

var d = parseFloat(pants[3]);
var e = 1.56;
var answer = (d / e).toFixed(3);

Be careful, though, because toFixed returns a string, not a number.  If you only need to display the answer, then you can use the above.  However, if you plan on using the variable answer elsewhere for calculations, you can use the following.  You just parse the string one more time to get a number again.

var d = parseFloat(pants[3]);
var e = 1.56;
var answerStr = (d / e).toFixed(3);
var answer = parseFloat(answer);

Either way, hope that helps.
0
 
Martin CotterillDabblerAuthor Commented:
Hi dakyd

Great answer.

The first example is good for me, but I get a period (.) and I need a comma (,). What I get is, for example, 233.974 and I need is 233,974.

I've found some code that changes the format, but it doesn't work with your coding. I put it here for reference. When I put the amount in for var d it worked fine, but with the parseFloat... it has no effect and as it makes no sense to me, I don't know what to change.

function number_format(n) {
  var arr=new Array('0'), i=0;
  while (n>0)
    {arr[i]=''+n%1000; n=Math.floor(n/1000); i++;}
  arr=arr.reverse();
  for (var i in arr) if (i>0) //padding zeros
    while (arr[i].length<3) arr[i]='0'+arr[i];
  return arr.join();
}

Any thoughts.

Pantyboy
0
 
dakydCommented:
Whoa, I don't think you need anything nearly that complicated.  All you want is to replace a single period with a comma.  You can do that with a single call to replace().  That function does what it sounds like - it replaces a substring with a different substring.  Give this a shot, hope it helps:

var num = 1.0;
var denom = 2.56;
var rounded = (num / denom).toFixed(3);
rounded = rounded.replace(/\./g, ",");
alert(rounded);
0
 
wzd3Commented:
If you want to multiply by 1000, why not just use *1000, and then round it.  I suspect pants[3] is in thousands, not dollars.

The equation you had way above,  Math.round(d / e), could be used in the final step, if earlier you did:

var d = 1000 * parseFloat(pants[3]);
0
 
dakydCommented:
Not sure if you're actually trying to multiply by 1000, but I just realized that I accidentally posted the wrong thing in my last post.  My suggestion was this, that other post has the right idea, but doesn't incorporate the pants[3] bit.  Sorry 'bout that, didn't notice before.  Hope it helps.

var d = parseFloat(pants[3]);
var e = 1.56;
var answer = (d / e).toFixed(3);
rounded = answer.replace(/\./g, ",");
alert(rounded);
0
 
Martin CotterillDabblerAuthor Commented:
Hi both

Sorry for taking so long to reply, but that's timezones for you.

wzd3, thanks for the reply but it didn't work in this instance.

dakyd, my man! Yep, that did it. You are becoming my go-to javascript expert. Thanks again Sam, nice one.


Regards

Martin
0
 
Martin CotterillDabblerAuthor Commented:
Hi dakyd

We posted at the same time.

Thanks to the way you explain things, it was fairly easy to work out. Once again, many thanks.

P
0
 
dakydCommented:
Haha, good thing you understood it, ya kept me from looking silly for posting something else.  Anyhow, glad to hear you got what you wanted.  And thanks for the points.  =p
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now