TShopping

 找回密碼
 註冊
搜索
查看: 1237|回復: 1

[教學] 複利計算網頁程式碼

[複製鏈接]
發表於 2008-9-23 23:09:37 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
看得懂嗎!還真的有些複雜噢!

參考一下就好!!... 共分成二個部份

////////////////////////////////////////////////

1.選取程式碼:javascript放在網頁 部份
  1. <script language="javascript">



  2. function showMenuIndicator()

  3. {

  4. if (!document.images) return;

  5. var i;

  6. for (i = 0; i < document.images.length; i++)

  7. {

  8. if (document.images.name)

  9.   if (location.href.indexOf("/" + document.images.name + ".") >= 0)

  10.   document.images.src = "/images/menu/menuindicator.gif";

  11. }

  12. }

  13. function getCtrlVal(ctrl)

  14. {

  15. if (ctrl.value != null)

  16. {

  17.   return ctrl.value;

  18. }

  19. else if (ctrl.selectedIndex != null)

  20. {

  21.   if (ctrl.selectedIndex >= 0)

  22.    return ctrl.options[ctrl.selectedIndex].value;

  23. }

  24. else if (ctrl.length != null)

  25. {

  26.   var i;

  27.   for (i = 0; i < ctrl.length; i++)

  28.   {

  29.    if (ctrl.checked) return ctrl.value;

  30.   }

  31. }

  32. return null;

  33. }



  34. function setCtrlVal(ctrl,val)

  35. {

  36. if (ctrl.value != null)

  37. {

  38.   ctrl.value = val;

  39. }

  40. else if (ctrl.selectedIndex != null)

  41. {

  42.   var i;

  43.   for (i = 0; i < ctrl.length; i++)

  44.   {

  45.    if (ctrl.options.value == val)

  46.    {

  47.     ctrl.selectedIndex = i;

  48.     break;

  49.    }

  50.   }   

  51. }

  52. else if (ctrl.length != null)

  53. {

  54.   var i;

  55.   for (i = 0; i < ctrl.length; i++)

  56.   {

  57.    if (ctrl.value == val)

  58.    {

  59.     ctrl.checked = true;

  60.     break;

  61.    }

  62.   }

  63. }

  64. }



  65. function getHrefParam(paramName)

  66. {

  67. var i = location.href.indexOf("?" + paramName + "=");

  68. if (i < 0) i = location.href.indexOf("&" + paramName + "=");

  69. if (i < 0) return null;



  70. var j = location.href.indexOf("&", i + 1);

  71. if (j < 0) j = location.href.length;

  72. return unescape(location.href.substring(i + 2 + paramName.length, j));

  73. }



  74. function zeroBlanks(formname)

  75. {

  76. var i, ctrl;

  77. for (i = 0; i < formname.elements.length; i++)

  78. {

  79.   ctrl = formname.elements;

  80.   if (ctrl.type == "text")

  81.   {

  82.    if (makeNumeric(ctrl.value) == "")

  83.     ctrl.value = "0";

  84.   }

  85. }

  86. }



  87. function filterChars(s, charList)

  88. {

  89. var s1 = "" + s; // force s1 to be a string data type

  90. var i;

  91. for (i = 0; i < s1.length; )

  92. {

  93.   if (charList.indexOf(s1.charAt(i)) < 0)

  94.    s1 = s1.substring(0,i) + s1.substring(i+1, s1.length);

  95.   else

  96.    i++;

  97. }

  98. return s1;

  99. }



  100. function makeNumeric(s)

  101. {

  102. return filterChars(s, "1234567890.-");

  103. }



  104. function numval(val,digits,minval,maxval)

  105. {

  106. val = makeNumeric(val);

  107. if (val == "" || isNaN(val)) val = 0;

  108. val = parseFloat(val);

  109. if (digits != null)

  110. {

  111.   var dec = Math.pow(10,digits);

  112.   val = (Math.round(val * dec))/dec;

  113. }

  114. if (minval != null && val < minval) val = minval;

  115. if (maxval != null && val > maxval) val = maxval;

  116. return parseFloat(val);

  117. }



  118. function formatNumber(val,digits,minval,maxval)

  119. {

  120. var sval = "" + numval(val,digits,minval,maxval);

  121. var i;

  122. var iDecpt = sval.indexOf(".");

  123. if (iDecpt < 0) iDecpt = sval.length;

  124. if (digits != null && digits > 0)

  125. {

  126.   if (iDecpt == sval.length)

  127.    sval = sval + ".";

  128.   var places = sval.length - sval.indexOf(".") - 1;

  129.   for (i = 0; i < digits - places; i++)

  130.    sval = sval + "0";

  131. }

  132. var firstNumchar = 0;

  133. if (sval.charAt(0) == "-") firstNumchar = 1;

  134. for (i = iDecpt - 3; i > firstNumchar; i-= 3)

  135.   sval = sval.substring(0, i) + "," + sval.substring(i);



  136. return sval;

  137. }



  138. function presentValue(fv,r,y)

  139. {

  140. return fv/Math.pow(1+r,y);

  141. }



  142. function futureValue(p,r,y)

  143. {

  144. return p*Math.pow(1+r,y);

  145. }



  146. function returnRate(pv,fv,y)

  147. {

  148. return Math.pow(fv/pv,1.0/y) - 1.0;

  149. }



  150. function geomSeries(z,m,n)

  151. {

  152. var amt;

  153. if (z == 1.0) amt = n + 1;

  154. else amt = (Math.pow(z,n + 1) - 1)/(z - 1);

  155. if (m >= 1) amt -= geomSeries(z,0,m-1);

  156. return amt;

  157. }



  158. function basicInvestment(p,r,y,c)

  159. {

  160. if (c == null) c = 0;



  161. return futureValue(p,r,y) + c*geomSeries(1+r,1,y);

  162. }



  163. function annuityPayout(p,r,y)

  164. {

  165. return futureValue(p,r,y-1)/geomSeries(1+r,0,y-1);

  166. }



  167. function mortgagePayment(p,r,y)

  168. {

  169. return futureValue(p,r,y)/geomSeries(1+r,0,y-1);

  170. }



  171. function randN(m,s)

  172. {

  173. return s*Math.sqrt(-2*Math.log(Math.random()))*Math.cos(2*Math.PI*Math.random()) + m;

  174. }



  175. function logNmean(m,s)

  176. {

  177. return Math.log(m) - (Math.pow(logNsigma(m,s),2)/2);

  178. }



  179. function logNsigma(m,s)

  180. {

  181. return Math.sqrt(Math.log(Math.pow(s/m,2) + 1));

  182. }



  183. function gmEst(r_am,s)

  184. {

  185. return Math.sqrt(Math.pow(1 + r_am, 2) - Math.pow(s,2)) - 1;

  186. }



  187. function numOrder(n, m)

  188. {

  189. return n - m;

  190. }

  191. //-->





  192. </script>
複製代碼

----------------------------------------------------

////////////////////////////////////////////////

2.選取程式碼: 部份
-----------------------------------------------------
  1. <FORM NAME="mainform" ACTION="(script removed)doCalc()" METHOD="OST">

  2. <CENTER>

  3. <TABLE BGCOLOR="#f08080" BORDER="0" CELLPADDING="1" CELLSPACING="0"><TR><TD>

  4. <TABLE WIDTH="500" BORDER="0" CELLSPACING="0" BGCOLOR="#ffa500">

  5. <TR><TD COLSPAN=2>

  6. <TABLE WIDTH="100%" BORDER=0 CELLSPACING=0>

  7. <TR BGCOLOR="#008b8b"><TD ALIGN="center"><FONT COLOR="#FFFFFF"><B>輸入</B></FONT></TD></TR>

  8. </TABLE>

  9. </TD>

  10. </TR>

  11. <TR><TD width="207">本金:</TD>

  12. <TD width="289">$ <INPUT TYPE="TEXT" NAME="p" SIZE="10" VALUE="" onChange="value=formatNumber(value,2,0)">

  13. </TD></TR>

  14. <TR><TD width="207">利率:</TD>

  15. <TD width="289">  <INPUT TYPE="TEXT" NAME="r" SIZE="8" VALUE="5" onChange="value=numval(value,null,0)"> %

  16. </TD></TR>

  17. <TR><TD width="207">期數:</TD>

  18. <TD width="289">  <INPUT TYPE="TEXT" NAME="y" SIZE="8" VALUE="1" onChange="value=numval(value,null,0)">

  19. </TD></TR>

  20. <TR><TD COLSPAN=2 ALIGN="CENTER"> <BR>

  21. <INPUT TYPE="BUTTON" VALUE="計算" onClick="doCalc()"></TD></TR>

  22. <TR><TD COLSPAN=2>

  23. <TABLE WIDTH="100%" BORDER=0 CELLSPACING=0>

  24. <TR BGCOLOR="#008b8b"><TD ALIGN="center"><FONT COLOR="#FFFFFF"><B>未來值</B></FONT></TD></TR> </TABLE>

  25. </TD></TR>



  26. <TR><TD COLSPAN=2>複利周期: P(1 + r/n)<SUP>Yn</SUP>   n 等於…… </TD></TR>



  27. <TR><TD width="207">    1(每年)</TD>

  28. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_1" SIZE="10" VALUE="" readonly></TD></TR>

  29. <TR><TD width="207">   

  30. <INPUT TYPE="TEXT" NAME="n" SIZE="4" VALUE="4" onChange="value=numval(value,0,1)">(每季)</TD>

  31. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_n" SIZE="10" VALUE="" readonly>

  32. </TD></TR>

  33. <TR><TD width="207">    12(每月)</TD>

  34. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_12" SIZE="10" VALUE="" readonly></TD></TR>

  35. <TR><TD width="207">    365(每日)</TD>

  36. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_365" SIZE="10" VALUE="" readonly></TD></TR>



  37. <TR><TD width="207">    365 x 24(每小時)</TD>

  38. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_hourly" SIZE="10" VALUE="" readonly></TD></TR>

  39. <TR><TD COLSPAN=2>持續複利:</TD></TR>

  40. <TR><TD width="207">    Pe<SUP>Yr</SUP></TD>

  41. <TD width="289">$ <INPUT TYPE="TEXT" NAME="fv_e" SIZE="10" VALUE="" readonly></TD></TR>

  42. </TABLE></TD></TR></TABLE></CENTER></FORM>

  43. <SCRIPT LANGUAGE="JavaScript">

  44. <!--

  45. showMenuIndicator();



  46. function doCalc()

  47. {

  48. var p = numval(document.mainform.p.value);

  49. var r = numval(document.mainform.r.value)/100;

  50. var y = numval(document.mainform.y.value);

  51. var n = numval(document.mainform.n.value);



  52. document.mainform.fv_1.value = formatNumber(futureValue(p, r, y), 2);

  53. document.mainform.fv_n.value = formatNumber(futureValue(p, r/n, y*n), 2);

  54. document.mainform.fv_12.value = formatNumber(futureValue(p, r/12, y*12), 2);

  55. document.mainform.fv_365.value = formatNumber(futureValue(p, r/365, y*365), 2);

  56. document.mainform.fv_hourly.value = formatNumber(futureValue(p, r/8760, y*8760), 2);

  57. document.mainform.fv_e.value = formatNumber(p*Math.exp(r*y), 2);

  58. }

  59. //-->

  60. </SCRIPT>
複製代碼

範例: 複利計算參考

http://www.wellsli.com/interest_rate.htm

 

臉書網友討論
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-10 21:12 , Processed in 0.051312 second(s), 18 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表