View Full Version : Obj to convert integer to string and add decimal?

T Chap
12-20-2009, 09:27 AM
Is there an object that will convert a decimal to a string for LCD purposes as shown. There always needs to be 4 decimals even if 0's, but the integer doesn't contain any decimals. There are no resources for cogs for floating point. I was looking at the Numbers obj but haven't figured out it ToStr is the right thing to accomplish this.

714875 to "71.4875"

Thanks for any obj suggestions.


I worked out a simple trick to destruct the integer and construct the string:

int := (int * 10000) + (deci * 625)
int := ((int * 9) / 5) + 320_000
'ser.decf(3, int, 6)
'ser.str(3, string("F"))
F := int/10000 ' extract the two digits to the left of the decimal
ser.dec(3, F) 'display the two left digits
ser.str(3, string(".")) 'display the decimal point
int := int - (F*10000) 'subtract the two left digits from the total and display the 4 digits to the right
ser.decf(3, int, 4)

Post Edited (Todd Chapman) : 12/20/2009 3:02:12 AM GMT

Mike Green
12-20-2009, 12:28 PM
Your trick certainly would work.

You could also modify the decimal output routine in the Numbers object or any other object with a similar routine. These all build the number from left to right. You can simply check in this loop for a particular number of decimal places and output a decimal point. There's a variable that indicates whether a significant digit has been seen and you could also force that to true (or whatever) so that the routine zero fills from then on.