Word To String with decimal point

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
zan
Posts: 434
Joined: 15 May 2007 19:35

Word To String with decimal point

#1 Post by zan » 19 Dec 2021 11:14

Hello experts

I need to insert decimal point into string which is converted from word variable.
String will be transmitted via serial port to terminal software for monitoring.
For this project I'm using old mikroBasic v7.0.0.2 (not PRO version).
I have writen this code and it is working as expected, but do you have more "elegant" solution for this problem?

Image

Code: Select all

sub procedure WordToStrWithDecimalPoint(dim wInput as word, dim byref sOut as string[6])
    dim sTemp as string[5]
    sOut = ""

    WordToStr(wInput, sTemp)

    if (wInput >= 0) and (wInput < 10) then
        sOut[0] = " "
        sOut[1] = " "
        sOut[2] = "0"
        sOut[3] = "."
        sOut[4] = "0"
        sOut[5] = sTemp[4]
    end if
    
    if (wInput >= 10) and (wInput < 100) then
        sOut[0] = " "
        sOut[1] = " "
        sOut[2] = "0"
        sOut[3] = "."
        sOut[4] = sTemp[3]
        sOut[5] = sTemp[4]
    end if

    if (wInput >= 100) and (wInput < 1000) then
        sOut[0] = " "
        sOut[1] = " "
        sOut[2] = sTemp[2]
        sOut[3] = "."
        sOut[4] = sTemp[3]
        sOut[5] = sTemp[4]
    end if

    if (wInput >= 1000) and (wInput < 10000) then
        sOut[0] = " "
        sOut[1] = sTemp[1]
        sOut[2] = sTemp[2]
        sOut[3] = "."
        sOut[4] = sTemp[3]
        sOut[5] = sTemp[4]
    end if

    if (wInput >= 10000) and (wInput <= $FFFF) then
        sOut[0] = sTemp[0]
        sOut[1] = sTemp[1]
        sOut[2] = sTemp[2]
        sOut[3] = "."
        sOut[4] = sTemp[3]
        sOut[5] = sTemp[4]
    end if

    sOut[6] = 0
end sub

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: Word To String with decimal point

#2 Post by Thomas.Pahl@t-online.de » 19 Dec 2021 16:19

there are string function to achieve this:

str_insert_chr inserts character

length gives the length of a string

perhaps you get an idea?

you can calculate the index of a char in the brackets

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Word To String with decimal point

#3 Post by janni » 20 Dec 2021 01:59

You could use WordToStrWithZeros rather than WordToStr, then insert decimal dot as Thomas suggested at index 3, and then replace the zeroes you do not need (if any at index 0 and 1) with spaces.

zan
Posts: 434
Joined: 15 May 2007 19:35

Re: Word To String with decimal point

#4 Post by zan » 20 Dec 2021 18:42

Thanks for your suggestions, I will try something.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Word To String with decimal point

#5 Post by janni » 20 Dec 2021 19:02

It's really simple:

Code: Select all

sub procedure WordToStrWithDecimalPoint(dim wInput as word, dim byref sOut as string[6])

    WordToStrWithZeros(wInput, sOut)
    str_insert_chr(sOut,".",3)

    if sOut[0] = "0" then
       sOut[0] = " "
       if sOut[1] = "0" then
          sOut[1] = " "
       end if
    end if

end sub

zan
Posts: 434
Joined: 15 May 2007 19:35

Re: Word To String with decimal point

#6 Post by zan » 20 Dec 2021 19:36

Yes, I got the similar result, except that the 'str_insert_chr' procedure does not exist in the old mikroBasic.

Code: Select all

sub procedure WordToStrWithDecimalPoint(dim wInput as word, dim byref sOut as string[6])
    dim sTemp as string[5]
    sOut = ""

    WordToStrWithZeros(wInput, sTemp)
    
    sOut[0] = sTemp[0]
    sOut[1] = sTemp[1]
    sOut[2] = sTemp[2]
    sOut[3] = "."
    sOut[4] = sTemp[3]
    sOut[5] = sTemp[4]
    
    if sOut[0] = "0" then
        sOut[0] = " "
        
        if sOut[1] = "0" then
            sOut[1] = " "
        end if
    end if

    sOut[6] = 0
end sub

Post Reply

Return to “mikroBasic PRO for PIC General”