473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
File Type: zip sprintf.zip (42.3 KB, 112 views)
Mar 12 '10 #1
4 3768
Banfa
9,065 Expert Mod 8TB
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"
Mar 12 '10 #2
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.

Expand|Select|Wrap|Line Numbers
  1. char * formatHours(int seconds)
  2. { // convert integer seconds timer to string: " HH:MM"
  3.   byte hrs, mins ;
  4.   int fullmins ;
  5.   char time[7] = " " ;  // leading space
  6.  
  7.  
  8.   fullmins = seconds / 60 ; // convert seconds to minutes
  9.   hrs =  fullmins / 60 ;    // convert minutes into hours
  10.   mins = fullmins % 60 ;    // remainder of same division is minutes
  11.  
  12.   sprintf(txt4, "%02d", hrs) ;    // format 2 places, leading zero
  13.   strcat(time, txt4) ;   // add to time string
  14.   strcat(time, ":") ;    // add a colon
  15.  
  16.   sprintf(txt4, "%02d", mins) ;  // format 2 places, leading zero
  17.   strcat(time, txt4) ; //
  18.  
  19.   return (strcpy(txt7, time)) ;
  20. }
Mar 13 '10 #3
jkmyoung
2,057 Expert 2GB
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.
Mar 15 '10 #4
Banfa
9,065 Expert Mod 8TB
Thread Closed
The subject of getting formatHours working is continued in

this thread
Mar 15 '10 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Francesco | last post by:
Ciao, I'm new in c++ programming and I've experimented some method to convert real number into c++ string object, at present I've found as best solution use the C command "sprintf":...
6
by: Flyingaway | last post by:
I am just wondering what is the most elegant and efficient way to do such type conversion. Thanks, Flyingway
1
by: Bell, Kevin | last post by:
I'm pulling a range of cells from Excel into a list and the data in Excel is a number or possibly some text like an asterisk. Each member of the list is a com object (I think) and I'm converting...
8
by: ern | last post by:
Anybody know the Linux equivalent (or even better some C standard equivalent) for _gcvt() ? I want my float to become a char*. Thanks!
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
16
by: akarui.tomodachi | last post by:
What is the most easiest way to convert an integer value to ASCII character format ? I tried with sprintf(). It works. Is there any other way to do that ? Objective:: I like to convert an...
43
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
7
by: wenmang | last post by:
what is format for sprintf to convert long long integer (64 bits) to string?
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.