Take the 2-minute tour ×

I'm building a fairly calculation-heavy cart for a fabric store and have found myself needing to do a calculation on user inputted length * the baseprice per metre, but then checking the result to see if it is a multiple of the pattern length. If it is not a multiple, I need to find the closest multiple of the pattern length and change the result to that.

I need to also be able to do exactly the same calculation in PHP, but if anyone can help me out with the maths I can port anything that needs to be translated myself.

I am using jQuery 1.6.2 and already have the first part of the calculation done, I just need to check the result of (metres*price) against the pattern length.

Any help greatly appreciated

EDIT: These calculations all involve 2 decimal places for both the price and the pattern length. User inputted length may also contain decimals.

share|improve this question
Don't use floating point arithmetic for money. Convert the prices to cents. –  Felix Kling Aug 12 '11 at 9:21
i will be, everything is floating point atm because that's how it comes out of the db :( –  jammypeach Aug 12 '11 at 9:28
add comment

5 Answers

up vote 9 down vote accepted

Use the % (modulus) operator in Javascript and PHP, which returns the remainder when a is divided by b in a % b. The remainder will be zero when a is a multiple of b.


var result = userLength * basePrice;     //Get result
if(result % patternLength){              //Check if there is a remainder
  var remainder = result % patternLength; //Get remainder
  if(remainder >= patternLength / 2)      //If the remainder is larger than half of patternLength, then go up to the next mulitple
    result += patternLength - remainder;
  else                                    //Else - subtract the remainder to go down
    result -= remainder;
result = Math.round(result * 100) / 100;  //Round to 2 decimal places
share|improve this answer
facepalm :) i had though of modulus but didnt think through what i could do with the remainder. this works for me, thanks muchly. –  jammypeach Aug 12 '11 at 9:26
add comment
//roundDown to 2 decimal places function
function r2(n) {
   return Math.floor(n*100)/100; 

neededLength = 4.55;
price = 4.63;
patternLength = 1.6;

// price despite the length of the pattern
priceSum = r2(neededLength * price);

// calculate how many pattern lengths there must be to fit into needed length
patternLengths = Math.floor((neededLength+patternLength)/patternLength);
// calculate price for pattern lengths
priceByPatternSum = r2((patternLengths * patternLength) * price );
share|improve this answer
add comment

In javascript there is the remainder operator (similar to most languages with a c-like syntax).

Let x = length and y = price and z = product of x*y

var remainder = (z % x) / 100;

if (remainder === 0) {
   // z is a multiple of x

To get the closest x multiple to your result z you could round the result up (or down) using ceil or floor functions in the Math library.

if (r >= x / 2) {
    a = Math.ceil(z/x)*x;
else {
    a = Math.floor(z/x)*x;

Then round to two decimal places

Math.round(a / 100) * 100;
share|improve this answer
add comment

Not sure if I really understood the task as it seems quite simple to me, but have a look at this PHP code:

// --- input ---
$pattern = 12.34;
$input = 24.68;
$precision = 2; // number of decimals

// --- calculation ---

// switch to "fixed point":
$base = pow(10, $precision);
$pattern = round($pattern * $base);
$input = round($input * $base);

if ($input % $pattern) {
  // not an exact multiple
  $input = ceil($input / $pattern) * $pattern;
} else {
  // it is an exact multiple

// back to normal precision:
$pattern /= $base;
$input /= $base;

This can be easily translated to JavaScript.

$input will be the next closest multiple of the pattern. If you just need that and don't need to know if it actually was a multiple you could also simply do something like this:

$input = ceil($input * 100 / $pattern) * $pattern / 100;
share|improve this answer
add comment

You can use the modulus to find the remainder after a division and then if the remainder is equal to zero the it's a multiple.

//x and y are both integers
var remainder = x % y;
if (remainder == 0){
//x is a multiple of y
} else {
//x is not a multiple of y

If the numbers your using could be to 2dp, the modulus should still work, if not, multiply both by 100 first then carry out the above check.

share|improve this answer
Just remember to round the number so you don't get any floating point problems like stackoverflow.com/questions/3966484/… –  Teodor Aug 12 '11 at 9:19
add comment

Your Answer


