JavaScript doesn’t have a lot of high-level mathematical functions built-in and for a project I am/was working on, I needed to change the quantities of all the ingredients in a listed recipe when a user changed the value of the “Serves” drop down box. For example:

Changing the drop down to 5 would require a 25% increase from the ORIGINAL values, resulting in:

You should note that this is not a 100% accurate conversion. For the sake of readability, I use Math.round several times. So in cases where a fraction might be 64/100 it is first rounded to 60/100, reduced to 6/10 then simplified to 3/5. So four one hundredths (.04) accuracy is lost in that conversion. If you need higher precision you can remove the round functions to produce similar results at the cost of losing readability.
//function to get highest common factor of two numbers (a fraction) function HCF(u, v) { var U = u, V = v while (true) { if (!(U%=V)) return V if (!(V%=U)) return U } } //convert a decimal into a fraction function fraction(decimal){ if(!decimal){ decimal=this; } whole = String(decimal).split('.')[0]; decimal = parseFloat("."+String(decimal).split('.')[1]); num = "1"; for(z=0; z<String(decimal).length-2; z++){ num += "0"; } decimal = decimal*num; num = parseInt(num); for(z=2; z<decimal+1; z++){ if(decimal%z==0 && num%z==0){ decimal = decimal/z; num = num/z; z=2; } } //if format of fraction is xx/xxx if (decimal.toString().length == 2 && num.toString().length == 3) { //reduce by removing trailing 0's decimal = Math.round(Math.round(decimal)/10); num = Math.round(Math.round(num)/10); } //if format of fraction is xx/xx else if (decimal.toString().length == 2 && num.toString().length == 2) { decimal = Math.round(decimal/10); num = Math.round(num/10); } //get highest common factor to simplify var t = HCF(decimal, num); //return the fraction after simplifying it return ((whole==0)?"" : whole+" ")+decimal/t+"/"+num/t; }
post categories
- AJAX (2)
- css (2)
- freelance website design (16)
- graphics (4)
- javascript (4)
- photoshop (1)
- php/mysql (5)
- portfolio (2)
- resources (4)
- site info (2)
- social networking (1)
- tools (1)
- tutorials (8)
Limited time offer
As a limited time offer - thanks to a deal with Host Gator - Wildreason Productions is able to provide a basic 2-3 page webpage with a blog and custom logo for free. All you have to do is host your website at Host Gator as a new customer. Click here to signup or get more information about a free website!February 2012 M T W T F S S « Apr 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Tags
16x16 icon ajax backgrounds convert decimal convert fraction css css layout tutorial decimal to fraction desktop background dynamic width error reporting favicon favicon generator fixed width css fixed width layout fraction common denominator free backgrounds free favicon freelance website design graphics high resolution backgrounds image hover effect image rollover image transition invalid argument supplied foreach javascript javascript image transition liquid layout mootools mootools hover mootools transition effect networking online tool PHP png quote generator quotes resources runtime error simplify fraction social icon social networking tutorial wallpaper website designers



