physphil / UnitConverterUltimate

A simple, lightweight unit converter for Android
Apache License 2.0
225 stars 86 forks source link

Feature Request: Decimal/Fraction Conversion #144

Open trymeouteh opened 5 years ago

trymeouteh commented 5 years ago

0.5 = 1/2 2.25 = 9/4

wagb283 commented 3 years ago

`//decimal to fraction (that can be read on tape measure) //ported from a C prog I wrote in 2010, maybe Phil can use it. ///////////////////////////////////////////////////////
function dtof(dnum)
{ var num = 0; var tfr = 0.0; var twn = 0; var r = CV.res; //denom resolution: 64, 32, 16, etc. var dec = 0.0; var outf= "";

//get whole number twn=Math.floor(dnum);
//get fraction tfr =Number((dnum-twn).toFixed(CV.dres));

//mul fraction by resolution-cap, round to int num=Math.round(tfr*r);

//reduce to lowest frac, mod-2 (64,32,16,8,4,2), while((num%2==0)&&(r%2==0)){
num/=2;
r /=2; } //if no whole number, it's ok, just show fraction if(num==0)r=0;

//format output outf=twn+"-"+num+"/"+r;

//return to caller() return outf;
}//dtof
`

Feed it any decimal (22.345) and it will return a formatted string representing a 'tape measure' shop-fraction (22-11/32 in this case). If you change the resolution (in CV.res, for instance), it will re-calculate the value to that denominator (if possible).

I recommend staying with 1/64, as it is designed to walk the fraction down to the lowest denominator consistent with the input value and chosen resolution. I originally made this to convert mm and dec vals to something relatable to a 1/16th ruled tape measure when buying metric parts online.

ckujau commented 3 years ago

Needed something like this today. Measured something like 34.925 mm today of something made in the US and I knew the same measurement would be equal to some nice "round" number when expressed in inch (spoiler: it's 1 3/8 inch). So, displaying fractions would be a cool feature indeed ;-)

robinpaulson commented 3 years ago

I've got a related use case: say I need to drill a hole which is 13/64" diameter, which metric drill size do I need? Being able to input fractional inches into the app would be very useful.

For simple fractions (like 3/8), I can convert to a decimal number in my head, but 64ths are a bit much for my simple brain.

wagb283 commented 3 years ago

I did this just for fun as an exercise for win32 stuff eleven years ago. I use it all the time for parts ordering, drag it on top of the browser while looking for sizes, diameters, etc. to get an instant idea of sizes.

These days I use mostly js since it runs anywhere there's a browser and I can use frameworks and libraries without having to 'roll my own' low level stuff like I did with a lot of win32 (although there's no substitute for native if you need performance).

The green button changes the resolution (64, 32, 16, etc.) The center icon changes between imperial and metric. The two small white arrows indicate the fractional reading on a tape. It calculates 'on the fly', as you enter values (no need to hit 'enter'). Values can be entered in either window. Config allows to set this always on top, or not.

shop-tape

robinpaulson commented 3 years ago

@wagb283 nice, the white arrows to visualise the measurement are a good touch as well

illdeletethis commented 1 year ago

fraction support is exactly what i would have needed today, agreed on the feature request