Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Kalkulator cen - zaokrągla podane sumy, Nie wiem dlaczego skrypt zaokrągla mi sumy
harykopar
post
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 22.01.2010

Ostrzeżenie: (0%)
-----


Witam,
mam fajny skrypt kalkulatora, z którym mam jednak smutny problem. Kiedy wpisuję ilość 9, zaokrągla mi do 10. Liczby 23 i 24 zaokrągla do 25, a od 75 do 99 do 100. Proszę o pomoc kogoś, dla kogo kod Javascript jest czytelniejszy.

Head
  1. <script LANGUAGE="JavaScript">
  2. <!-- Original: WarpGear Software (bbcalculator@warpgear.com) -->
  3. <!-- Web Site: <a href="http://www.warpgear.com/developer" target="_blank">http://www.warpgear.com/developer</a> -->
  4.  
  5. <!-- This script and many more are available free online at -->
  6. <!-- The JavaScript Source!! <a href="http://javascript.internet.com" target="_blank">http://javascript.internet.com</a> -->
  7.  
  8. <!-- Begin
  9. var max_units = 100; // quantities in excess of max_units all have the same unit price
  10. var currency = " PLN"; // currency sign used in 'formatMessage()'
  11. // Edit this function to reflect your discount prices!
  12. function getDiscountPrice(units) {
  13. // Note: It is important to work your way down from max to min amounts!
  14. if (units >= max_units) return 30.00;
  15. if (units >= 50) return 35.00;
  16. if (units >= 25) return 40.00;
  17. if (units >= 10.00) return 45.00;
  18. if (units >= 5.00) return 50.00;
  19. if (units >= 3.00) return 55.00;
  20. if (units >= 1.00) return 60.00;
  21. if (units <= 0.00) return 0.00;
  22. }
  23. function getNumberOfUnits() {
  24. var units = document.calculator.units.value;
  25. return (units == "") ? 0 : units;
  26. }
  27. function showResult(result) {
  28. // adjust the following line if result must popup somewhere else
  29. document.calculator.respons.value = result;
  30. }
  31. function formatMessage(units, unit_price) {
  32. return units + " * " + formatPrice(unit_price) + currency + " = " + formatPrice(units * unit_price) + currency;
  33. }
  34. // AltUnits (alternate units): add extra units to reach minimum for next discount price
  35. function getAltUnits(units) {
  36. var discount_price = getDiscountPrice(units);
  37. if (units < max_units) do { units++ } while (discount_price == getDiscountPrice(units));
  38. return units;
  39. }
  40. function findPrice() {
  41. var units = getNumberOfUnits();
  42. var unit_price = getDiscountPrice(units);
  43. var alt_units = getAltUnits(units);
  44. var alt_unit_price = getDiscountPrice(alt_units);
  45. var result;
  46. if ((units * unit_price) < (alt_units * alt_unit_price))
  47. result = formatMessage(units, unit_price);
  48. else
  49. result = formatMessage(alt_units, alt_unit_price);
  50. showResult(result);
  51. }
  52. function formatPrice(value) {
  53. var result= Math.floor(value);
  54. var cents = 100 * (value-Math.floor(value)) + 0.5;
  55. result += Math.floor(cents / 10);
  56. result += Math.floor(cents % 10);
  57. return result;
  58. }
  59. function filterNonNumeric(field) {
  60. var result = new String();
  61. var numbers = "0123456789";
  62. var chars = field.value.split(""); // create array
  63. for (i = 0; i < chars.length; i++) {
  64. if (numbers.indexOf(chars[i]) != -1) result += chars[i];
  65. }
  66. if (field.value != result) field.value = result;
  67. }
  68. // End -->


Body
  1. Ilość:
  2. <input type=text value="1" name="units" onkeydown="findPrice()" onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()" size="4"> sztuk
  3. <input type=text onfocus="this.blur()" name="respons" size="24" style="border:0; font-weight:bold;">


Swoją drogą fajny skrypt, polecam. Wylicza ceny za ilość sztuk i można wprowadzić zakres cen w zależności od ilości produktów.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
wookieb
post
Post #2





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




Robi to funkcja getAltUnits
A dlaczego to nie wnikam/
Go to the top of the page
+Quote Post
harykopar
post
Post #3





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 22.01.2010

Ostrzeżenie: (0%)
-----


Funkcja jest potrzebna, by skrypt działał. Ale jak wykasowałem z samej funkcji tylko wiersz:
  1. if (units < max_units) do { units++ } while (discount_price == getDiscountPrice(units));

wszystko działa super. Dzięki wielkie za pomoc wookieb!
Go to the top of the page
+Quote Post
Pawel_W
post
Post #4





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

Ostrzeżenie: (0%)
-----


Cytat
add extra units to reach minimum for next discount price

wszystko wyjaśnione w komentarzach (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 10:11