PDA

View Full Version : How do I add a decimal point?



polymer52
- 13th December 2009, 21:27
How do I break down a number to add a decimal point when I send it out to debug or a serial port?
Example: I have a number like 126 and I want to send out 12.6
Thanks.

Charles Linquis
- 13th December 2009, 23:35
HSEROUT [#(number/10),".",#(number//10)]

polymer52
- 14th December 2009, 01:12
That worked great..Thank you.
And for debug I used:


DEBUG 1," Channel 0: ",DEC(ad0/10),".",DEC(ad0//10),10,13

retepsnikrep
- 14th December 2009, 07:27
You can have a problem with leading zeros in the part after the decimal point, not sure if it effects PBP but it certainly effects picaxe basic.

Example

I want to display 102 as decimal with the . between 1 and 0

1.02

If you use the method suggested I expect the display will actually show

1.2 and not 1.02

It probably works correctly as long there is a number other than 0 present.

i.e.

122 would work and display 1.22

102 would not and would display 1.2 not 1.02.

Anyone care to comment.

Here is some picaxe code I use which can probably be adapted for this issue in PBP




VarC = Soc / 100 ;Get first part of value (Before decimal point)
VarA = Soc // 100 ;Get second part of value (After decimal point)

serout Video,BaudT9600,(27,83,0,5,"Soc ",#VarC,".") ;Video Display

if VarA < 10 then ;If Leading zero reqd in display due to Value <10 then
serout Video,BaudT9600,("0",#VarA," % ") ;Video Display
else
serout Video,BaudT9600,(#VarA," % ") ;Video Display
endif

mackrackit
- 14th December 2009, 11:47
122 would work and display 1.22

102 would not and would display 1.2 not 1.02.

Anyone care to comment.
YUP, I will comment..

The sample code Charles gave will return 10.2 if 102 is entered.
This will take the last two numbers, even if the numbers are both zero and put them behind the decimal point.

HSEROUT [DEC X/100,".",DEC2 X//100]