Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [js] zaogrąglanie
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
Sm0key
mam taki skrypt ::

  1. <meta http-equiv="Content-type" content="text/html; charset=iso-8859-2">
  2.  
  3. <script type="text/javascript" language="JavaScript">
  4. <!--
  5. function licz(){
  6. var input1 = parseInt(document.fakrura.vzero.value);
  7. var input2 = parseInt(document.fakrura.vdwa.value);
  8. var input3 = parseInt(document.fakrura.vsie.value);
  9. var input4 = parseInt(document.fakrura.vtrz.value);
  10.  
  11. if (!input1) { input1 = 0; } else {
  12. input1 = input1;
  13. }
  14.  
  15. if (!input2) { input2 = 0; } else {
  16. input2 = input2;
  17. }
  18.  
  19. if (!input3) { input3 = 0; } else {
  20. input3 = input3;
  21. }
  22.  
  23. if (!input4) { input4 = 0; } else {
  24. input4 = input4;
  25. }
  26. var vat0 = input1* 0.0;
  27. var vat22 = input2 * 0.22;
  28. var vat7 = input3 * 0.07;
  29. var vat3 = input4 * 0.03;
  30.  
  31. var razem = input1 + input2 + input3 + input4;
  32. var razem_v = vat0 + vat22 + vat7 + vat3 + input1 + input2 + input3 + input4;
  33.  
  34. document.fakrura.sumab.value = razem;
  35. document.fakrura.sumaz.value = razem_v;
  36. }
  37. //-->
  38. </head>
  39.  
  40.  
  41. <form action="" name="fakrura">
  42.  
  43.  
  44. 0% <input type="text" onBlur="licz()" name="vzero"/><br>
  45.  
  46. 22% <input type="text" onBlur="licz()" name="vdwa"/><br>
  47.  
  48. 7% <input type="text" onBlur="licz()" name="vsie"/><br>
  49.  
  50. 3% <input type="text" name="vtrz" onblur="licz()" ><br><br>
  51.  
  52.  
  53.  
  54.  
  55. razem netto<input type="text" onBlur="licz()" name="sumab"/><br>
  56.  
  57. razem brutto<input type="text" onBlur="licz()" name="sumaz"/><br>
  58.  
  59. </form>
  60.  
  61.  
  62. </body>
  63. </html>


i mam problem po wpisaiu wartosci 1 w inputach 0% i 22 % wylicza mi sume bez vat 2 a sume z vat 2.2199999999999997 . blink.gif

czy jesty coś zle questionmark.gif
jak w js zrobic zaokraglenia questionmark.gif
nospor
Cytat
//Returns the value 20
x=Math.round(20.49)

//Returns the value 21
x=Math.round(20.5)

//Returns the value -20
x=Math.round(-20.5)

//Returns the value -21
x=Math.round(-20.51)
Sm0key
Cytat(nospor @ 2005-08-12 11:46:34)
Cytat
//Returns the value 20
x=Math.round(20.49)

//Returns the value 21
x=Math.round(20.5)

//Returns the value -20
x=Math.round(-20.5)

//Returns the value -21
x=Math.round(-20.51)

TAK TYLKO MI BARDZI\EJ JEST POTRZEBNE Z DWOMA CYFRAMI PO PRZECINKU.

NIE WIEM DLACZEGO TAK LICZY dry.gif .

czasem sa dwie liczby po przecinku czasem jakis magiczny ulamek od czego to moze zalezec worriedsmiley.gif

1 + 1*1,22 = 2,22 a nie kosmiczny uamamek blink.gif
crash
Wystarczy pomnożyć składniki razy 100, użyć Math.round i podzielić przez 100. Gotowe - masz liczby zaokrąglone do 2-go miejsca po przecinku...
Sm0key
NAJPROSTSZE ZAWSZE NIEWIDOCZNE. DZIEKI smile.gif

ALE I TAK MNIE ZASTANAWIA JAK JS TO LICZY ZE MU TAKIE KOSMICZNE ULAMKI WYCHODZA .......
FiDO
Jak chcesz sie dowiedziec czemu to musialbys sie zaglebic w teorie z metod numerycznych.. jak bedziesz studiowal informatyke kiedys to sie dowiesz skad te "kosmiczne ulamki". W skrocie mowiac wynika to z nienajlepszej precyzji liczb zmiennoprzecinkowych.

PS. nie pisz duzymi literami, bo to oznacza krzyk, a chyba krzyczec nie musisz..
dasko
Number.toFixed(2). Tyle.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.