View Full Version : Converting from Scientific notation to set precision

01-20-2009, 12:04 PM
Hello! I has hoping someone could tell me the best way of converting a number from scientific notation to set precision. I'm reading voltage values in from an A\D converter using the floatMath.spin object to convert the value to floating point. I'm trying to set the output format to a set precision of 4 decimal places but having very little luck doing so. At this time the output I'm observing in the serial terminal window is as follows:

Current pH reading in decimals: 4.484155e-43

I would like to display the value 4.4842 since I'll eventually be dealing with millivolts. I'd greatly appreciate it if someone could point me in the right direction. Thank you!

Mike Green
01-20-2009, 12:19 PM
The FloatString object in the Object Exchange will do what you want. Download it and have a look. Note that you'll have to multiply by an appropriate power of ten to get the number to come out the way you want it. If it's 4.xxxxE-43, you'll have to multiply by 1E43 before you convert it to a string. If you don't want to do that, you'll have to do some string manipulation to strip off the "E-43" and do rounding on the characters in the string itself. It's not hard to do.

Post Edited (Mike Green) : 1/20/2009 5:25:48 AM GMT

01-20-2009, 03:44 PM
Thanks for the help, Mike. I was going through my code and realized my code was set up for a 12-bit A\D converter and not an 8-bit. After correcting that bit of code I was faced with another problem. I was unable to convert the integer output (0 - 255) to a floating point value. Suffice to say, back to square one. I used the floatMath.FFloat(integer) object method to convert the integer data type to float but to no avail. The snippet of code below is part of what I have:
repeat 8
value <<= 1
value += ina[12]

value := fMath.FFloat(value)

The value output I observe is anything but a floating point number. Any suggestions? Thanks!

01-20-2009, 10:00 PM
Why convert to floating point in the first place? The 8 bit number you are getting (call it x) is essentially (x/256)* adcref. The entire calculation can be done in binary arithmetic, converted to decimal (ascii) and printed. Much faster than going back and forth to floating point.

Erik Friesen
01-20-2009, 10:14 PM
I use this code all the time to place a artificial decimal point. All you have to do is cut and paste it into the terminal program.

For example, if x=12345

and you call it like this


It will print

123.45 and will clear 10 spaces total for printing in the same line.

PUB dec(value,dpoint,clearpoint) | i,a,b,c'with 6 digit floating point limiter

'' Print a decimal number
if value < 0
if value>99999 and clearpoint<7

i := 1_000_000_000
repeat a from 1 to 10
if a==b
if dpoint>0
c:=c-1 #>0
if value => i or a=>b
out(value / i + "0")
value //= i
c:=c-1 #>0
elseif result or i == 1
c:=c-1 #>0
i /= 10
repeat c
out(" ")

01-21-2009, 02:05 AM
Erik, thanks for the code! Exactly what I was looking for. I need to dynamically display pH values on an HTML page and pH values without decimal points just don't look right. Much thanks!