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
<script LANGUAGE="JavaScript"> <!-- Original: WarpGear Software (bbcalculator@warpgear.com) -->
<!-- Web Site: <a href="http://www.warpgear.com/developer" target="_blank">http://www.warpgear.com/developer</a> -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! <a href="http://javascript.internet.com" target="_blank">http://javascript.internet.com</a> -->
<!-- Begin
var max_units = 100; // quantities in excess of max_units all have the same unit price
var currency = " PLN"; // currency sign used in 'formatMessage()'
// Edit this function to reflect your discount prices!
function getDiscountPrice(units) {
// Note: It is important to work your way down from max to min amounts!
if (units >= max_units) return 30.00;
if (units >= 50) return 35.00;
if (units >= 25) return 40.00;
if (units >= 10.00) return 45.00;
if (units >= 5.00) return 50.00;
if (units >= 3.00) return 55.00;
if (units >= 1.00) return 60.00;
if (units <= 0.00) return 0.00;
}
function getNumberOfUnits() {
var units = document.calculator.units.value;
return (units == "") ? 0 : units;
}
function showResult(result) {
// adjust the following line if result must popup somewhere else
document.calculator.respons.value = result;
}
function formatMessage(units, unit_price) {
return units + " * " + formatPrice(unit_price) + currency + " = " + formatPrice(units * unit_price) + currency;
}
// AltUnits (alternate units): add extra units to reach minimum for next discount price
function getAltUnits(units) {
var discount_price = getDiscountPrice(units);
if (units < max_units) do { units++ } while (discount_price == getDiscountPrice(units));
return units;
}
function findPrice() {
var units = getNumberOfUnits();
var unit_price = getDiscountPrice(units);
var alt_units = getAltUnits(units);
var alt_unit_price = getDiscountPrice(alt_units);
var result;
if ((units * unit_price) < (alt_units * alt_unit_price))
result = formatMessage(units, unit_price);
else
result = formatMessage(alt_units, alt_unit_price);
showResult(result);
}
function formatPrice(value) {
var result= Math.floor(value);
var cents = 100 * (value-Math.floor(value)) + 0.5;
result += Math.floor(cents / 10);
result += Math.floor(cents % 10);
return result;
}
function filterNonNumeric(field) {
var result = new String();
var numbers = "0123456789";
var chars = field.value.split(""); // create array
for (i = 0; i < chars.length; i++) {
if (numbers.indexOf(chars[i]) != -1) result += chars[i];
}
if (field.value != result) field.value = result;
}
// End -->
Body
Ilość:
<input type=text value="1" name="units" onkeydown="findPrice()" onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()" size="4"> sztuk
<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.