Convert a rounded float to a string with sprintf?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nigelmercier
    New Member
    • Dec 2009
    • 45

    Convert a rounded float to a string with sprintf?

    I need to convert a float to a string, after rounding it to 1 decimal place.

    For example: float f = 1.264 the string should be "1.3".

    I have just discovered that I do have access to sprintf, although in my compiler it doesn't actually "print" anything (its on a PIC microcontroller ). The only problem is that I have no idea how to drive it!

    I've attached the relevant page from the help file: perhaps someone who knows this function could help.

    TIA
    Attached Files
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    sprintf doesn't "print" anything on any platform it outputs its results to a string.

    The help page you attached seems explicit and standard you will need to read and understand it if you have much more of this type of thing to do.

    In this case you need the format string "%.1f"

    Comment

    • nigelmercier
      New Member
      • Dec 2009
      • 45

      #3
      Having found sprintf, I realised that it was idea for my time conversion function, see this thread. So I took the plunge, and converted it, and it worked for a while, but now something has gone wrong!

      This function is passed an integer number of seconds, and returns a text string " HH:MM" derived from it. But it doesn't work anymore! Note that txt4[4] and txt7[7] are global scratchpad variables.

      Code:
      char * formatHours(int seconds)
      { // convert integer seconds timer to string: " HH:MM"
        byte hrs, mins ;
        int fullmins ;
        char time[7] = " " ;  // leading space
      
      
        fullmins = seconds / 60 ; // convert seconds to minutes
        hrs =  fullmins / 60 ;    // convert minutes into hours
        mins = fullmins % 60 ;    // remainder of same division is minutes
      
        sprintf(txt4, "%02d", hrs) ;    // format 2 places, leading zero
        strcat(time, txt4) ;   // add to time string
        strcat(time, ":") ;    // add a colon
      
        sprintf(txt4, "%02d", mins) ;  // format 2 places, leading zero
        strcat(time, txt4) ; //
      
        return (strcpy(txt7, time)) ;
      }

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        But it doesn't work anymore!
        What do you mean doesn't work? Does it throw errors, or give the wrong output?

        Have you retested it, eg, pass it 0 seconds, pass it 100 seconds, and see what time it gives you? Perhaps you are just now hitting upon base cases you had not considered.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Thread Closed
          The subject of getting formatHours working is continued in

          this thread

          Comment

          Working...