Am I right in assuming that if you needed it rounded to the nearest integer, you would just have to incf temp,F before the last rotate? This will make a valuable addition to my math.asm, if you will let me plagiarize :) ---------- > From: Scott Dattalo > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: Conversion of byte to percentage > Date: Thursday, May 21, 1998 1:39 PM > > Justin Crooks wrote: > > > > > From: Scott Dattalo > > > > > > > > > MOVWF temp ;Hex value is in W. > > > CLRC ;Clear the Carry bit so it doesn't > > > RRF temp,F > > > CLRC > > > RRF temp,F > > > CLRC > > > RRF temp,F ;temp = h>>3 > > > ADDWF temp,F ;temp = h + (h>>3) > > What happens if result > 0xFF? > > The carry will get set and the next RRF will pick it up like a sponge > soaks up water. > > > > RRF temp,F ;temp = (h + (h>>3)) >> 1 > > > ADDWF temp,F ;temp = h + ((h + (h>>3)) >> 1) > > Again, same issue. > > Again, same answer. > > > > RRF temp,F > > > CLRC > > > RRF temp,W ;d = W = (h + (h + (h>>3)) >> 1) >> 2 > > > > > > RETURN > > > > This is excellent, but will not work for values > 64%. I like how clean it > > is, though. You could add a line to check if MSB set. Subtract 128 if > > set, and add 50% at the end. > > The routine works just fine. Try it. The only limitation that it does > have is that it actually computes floor(x * 100 / 256). So if your > percentage is 88.7%, for example, you would get just 88%. For my > application this was okay. > > Scott