Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] How to adapt this routine for converting both float and int to string ?

Status
Not open for further replies.

Eric_O

Advanced Member level 4
Full Member level 1
Joined
May 31, 2020
Messages
107
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Activity points
1,024
Here under the subroutine in MikroC :

Code:
void print_float_v4(char *flt, long number, char decimals)
{
 if (number < 0)
    {
     number = - number;
     *(flt) = '-';
    }
    else
       {
        *(flt) = ' ';
       }

 *(flt + 1) = ' ';

 if (decimals == 2)
    {
     *(flt + 2) = number / 1000000 + 48;
     *(flt + 3) = ((number % 1000000) / 100000) + 48;
     *(flt + 4) = ((number % 100000) / 10000) + 48;
     *(flt + 5) = ((number % 10000) / 1000) + 48;
     *(flt + 6) = ((number % 1000) / 100) + 48;
     *(flt + 7) = ((number % 100) / 10) + 48;
     *(flt + 8) = ((number % 10) / 1) + 48;
     *(flt + 9) = ',';
     *(flt + 10) = ((number % 100) / 10) + 48;
     *(flt + 11) = (number % 10) + 48;
     *(flt + 12) = 0;
    }
    else
       {
        *(flt + 2) = number / 100000 + 48;
        *(flt + 3) = ((number % 100000) / 10000) + 48;
        *(flt + 4) = ((number % 10000) / 1000) + 48;
        *(flt + 5) = ((number % 1000) / 100) + 48;
        *(flt + 6) = ((number % 100) / 10) + 48;
        *(flt + 7) = ',';
        *(flt + 8) = (number % 10) + 48;
        *(flt + 9) = 0;
       }

 if (*(flt + 2) == '0')                                           // Suppression des zéros inutiles.
    {
     *(flt + 2) = ' ';
     if (*(flt + 3) == '0')
        {
         *(flt + 3) = ' ';
         if (*(flt + 4) == '0')
            {
             *(flt + 4) = ' ';
             if (*(flt + 5) == '0')
                {
                 *(flt + 5) = ' ';
                 if (*(flt + 6) == '0')
                    {
                     *(flt + 6) = ' ';
                     if (*(flt + 7) == '0')
                        {
                         *(flt + 7) = ' ';
                        }
                    }
                }
            }
        }
    }
}

Merci !
 

Hello!

Just write a function for float. Then if you have an int as an argument, it should be
automatically cast to the proper format.
Beside this, you are spending energy of things that already exist (there are already standard
conversions which would work in the case you have a decent amount of memory.
Then in the case you don't have a decent amount of memory, you are still spending
a lot of effort writing possibly unstable functions by processing byte by byte (already
said earlier). Why don't you use loops? Why limiting your code to decimals = 2 or another
value? Will the behavior be adequate if decimals = 1 or decimals = 3? What will happen if
you have more decimals? Etc, etc...

That said, there are very interesting features of C++ you may be interested to learn about,
and which would do that kind of automatic choice.

Best regards

Dora.
 

Hi,

I agree with Dora.

Your code is almost uncommented, thus it's rather hard to read. Especially for those who did not write the code ... but also for yourself, after some time.
Thus I recommend to add comments ... especially when posting on a forum.
And you may write your code to make it more "obvious" what you want to do.

Example:
You write " ... + 48"
With this you transform an integer number (0...9) into an ASCII character '0' ... '9'. So you add the integer value of an ASCII character "0", which is 48.
But you simply could write " + '0' "

Another benefit: neither you nor others need to look for the ASCII code of "0".

The compiler will not create different code, because it treats all the same: 48 = 0x30 = 0b00110000 = '0'.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top