473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript in Mac: Number.toFixed challenge

View Poll Results: Macs and javascript: to be or not to be, that is the question.
A match made in heaven, like summer & ice cream! 0 0%
Like McDonalds with healthy food, Mac's not there yet...but they are trying. 1 100.00%
Umm, something's wrong,...and I don't think they're trying. 0 0%
What's Mac got to do...(i said) got to do with it!?... 0 0%
Voters: 1. You may not vote on this poll

As Tom Cahill would require, my challenge is (according to the "three
R's")

Reproducible: if run on OS 9.x, the monthly payment does not show up in
the text box:

Recognizable: I believe the Mac OS 9.x is not getting the temp_var
value returned to the var MP to show in the form. My hypothesis is
that the problems lies in this function:

Expand|Select|Wrap|Line Numbers
  1.      function round_2(Number) 
  2.     {
  3.     var temp_var = (Number.toFixed(2))
  4.     return temp_var
  5.     }
  6.  
  7.  
Repairable:I believe I've located the error location, but this is my
2nd day coding Javascript, so I don't know if it's repairable. Is
there a fix to this? I heard if the Number.toFixed method isn't
available on the operating system, it is possible to write that method
as a function.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <HTML><HEAD><TITLE>Simple Mortgage Calculator</TITLE>
  3. <script language="JavaScript"> 
  4. function round_2(Number)
  5. {
  6. var temp_var = (Number.toFixed(2))
  7. return temp_var
  8. }
  9.  
  10. function find_payment(A, B, C)
  11. {
  12. var temp_var = (A * B) / (1 - Math.pow(1 + B, -C))
  13. return temp_var
  14. }
  15.  
  16. function dosum()
  17. {
  18. if(document.temps.PP.value>0)
  19. {
  20. if((document.temps.DP.value / document.temps.PP.value) <=0)
  21. {document.temps.DP.value=document.temps.PP.value/10}
  22. else if((document.temps.DP.value/document.temps.PP.value)<.1)
  23. {
  24. alert("A 10% Miminimum Down Payment Is Required.")
  25. document.temps.DP.value=document.temps.PP.value/10
  26. }
  27. else if((document.temps.DP.value/document.temps.PP.value)>=1)
  28. {
  29. alert("Come on. If you pay for the property in full at time of
  30. purchase, you won't need a loan. Why are you looking at terms? If you
  31. are going to pay in full, call me at (800) 760-3001 and I'll give you a
  32. 5% discount on the purchase of any property on our inventory.")
  33. document.temps.DP.value=document.temps.PP.value/10
  34. }
  35. else
  36. {document.temps.DP.value=document.temps.DP.value}
  37.  
  38. if(document.temps.YR.value<=0)
  39. {document.temps.YR.value=15}
  40. else if(document.temps.YR.value>30)
  41. {
  42. alert("30 Year Is The Maximum Repayment Period.")
  43. document.temps.YR.value=30
  44. }
  45. else
  46. {document.temps.YR.value=document.temps.YR.value}
  47.  
  48. document.temps.LA.value=document.temps.PP.value-document.temps.DP.value
  49. if((document.temps.DP.value/document.temps.PP.value)<.2)
  50. {document.temps.IR.value=.09}
  51. else if((document.temps.DP.value/document.temps.PP.value)<.3)
  52. {document.temps.IR.value=.085}
  53. else
  54. {document.temps.IR.value=.08}
  55. var MP = document.temps.MP.value
  56. var LA = document.temps.LA.value
  57. var IR = document.temps.IR.value
  58. var YR = document.temps.YR.value
  59. MP=find_payment(LA,IR/12,YR*12)
  60.  
  61. document.temps.AT.value=(document.temps.PP.value*.0125)
  62.  
  63. document.temps.MP.value=round_2(MP)
  64. }
  65. else
  66. {alert("Please Enter A Purchase Price")}
  67. }
  68. function clearboxes()
  69. {
  70. document.temps.PP.value = "";
  71. document.temps.DP.value = "";
  72. document.temps.LA.value = "";
  73. document.temps.YR.value = "";
  74. document.temps.IR.value = "";
  75. document.temps.MP.value = "";
  76. document.temps.AT.value = "";
  77. }
  78. // -->
  79. </script>
  80. </HEAD><b>Simple Mortgage Calculator</b><br>
  81. <br>
  82. <form name="temps">
  83. Loan Details...<br><br>
  84. <u>variable price</u>
  85. <br>Purchase Price:<input NAME="PP" onfiltered="dosum()" tabindex="1"
  86. size=15 style="font-size:9pt;">
  87. <hr width=95%><br><br>
  88. <u>terms are flexible</u>
  89. <br>Down Payment:<input NAME="DP" onfiltered="dosum()" tabindex="1"
  90. size=15 style="font-size:9pt;">
  91. <br>Repayment Period: (Years)<input NAME="YR" onfiltered="dosum()"
  92. tabindex="2" size=15 style="font-size:9pt;">
  93. <br>
  94. <br>
  95. Results...<br><br>
  96. <u>fixed numbers</u>
  97. <br>Monthly Payment:<input size=13 NAME="MP" style="font-size:9pt;"
  98. tabindex="8">
  99. <br>Loan Amount:<input NAME="LA" onfiltered="dosum()" tabindex="1"
  100. size=15 style="font-size:9pt;">
  101. <br>Interest Rate:<input size=15 style="font-size:9pt;" NAME="IR"
  102. "onfiltered="dosum()" tabindex="3">
  103. <br>Annual County Tax:<input size=13 NAME="AT" style="font-size:9pt;"
  104. tabindex="11">
  105. <br>
  106.  
  107. <br><br><blockquote><a href="javascript:clearboxes()" width=58
  108. height=16 border=0>Clear</a>
  109. <br><br><a href="javascript:dosum()" width=100 height=16
  110. border=0>CALCULATE</a>
  111. </blockquote>
  112. <div align=center><font size=1>Calculations made by this tool are
  113. believed to be accurate but are not guaranteed.</font></div>
  114. </BODY></HTML>
  115.  
Thanks for your help
Feb 7 '06 #1
5 6813
acoder
16,027 Expert Mod 8TB
You can add toFixed if it doesn't exist. Either use a function or add it directly using prototype, e.g.
Expand|Select|Wrap|Line Numbers
  1. if (!num.toFixed) {
  2.     Number.prototype.toFixed = function(precision) {
  3.         var num = (Math.round(this*Math.pow(10,precision))).toString();
  4.         return num.substring(0,num.length-precision) + "." + num.substring(num.length-precision, num.length);
  5.     }
  6. }
Apr 16 '08 #2
gits
5,390 Expert Mod 4TB
and one more thing to mention: the problem is not the MAC ... it is the browser you would use with it that has to have an implementation of ECMA-Script ... and so the problem is the browser that is used with a specific OS ... and from that point of view the OS could be a problem when it limits the use of an up to date browser or the user who wants to use a specific browser that you have to support - but not the MAC itself :) ...

just this two cents from a MAC-user ... :)

kind regards
Apr 16 '08 #3
acoder
16,027 Expert Mod 8TB
Yes, of course, you're absolutely right!

Forgot to mention that... thanks!

It's probably either IE5 on the Mac or an old version of Safari that's the problem.
Apr 16 '08 #4
mrhoo
428 256MB
While acoder's method is flawless, and gits remarks apt, I am not sure that toFixed is the problem. I didn't remember the 'onfilter ' event, which is the one calling the method, untitl I googled it,

onfilter is a non existent event handler that web based email services such as Yahoo mail uses in place of actual event handlers inside emails that contain a JavaScript. It's a security precaution, to disable any malicious script from executing in the user's browser.
This doesn't mean he has the method toFixed, and anyone who doesn't can use acoder's replacement, but what event calls dosum?
Apr 16 '08 #5
acoder
16,027 Expert Mod 8TB
Good spot! Goes to show that three (sets of) eyes are better than one...
what event calls dosum?
Probably via the CALCULATE link at the bottom, though onfiltered should probably be onchange.
Apr 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tom Cahill | last post by:
The following code will work perfectly on a PC browser, IE or Netscape. However on a Mac browser (IE, Netscape, or Safari) it does not. I have tried it on OSX and OS9. ANy help anyone can offer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.