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
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"
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. - 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)) ;
-
}
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.
Banfa 9,065
Expert Mod 8TB Thread Closed
The subject of getting formatHours working is continued in this thread This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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":...
|
by: Flyingaway |
last post by:
I am just wondering what is the most elegant and efficient way to do
such type conversion.
Thanks,
Flyingway
|
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...
|
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!
|
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...
|
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...
|
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...
|
by: wenmang |
last post by:
what is format for sprintf to convert long long integer (64 bits) to
string?
|
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...
|
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
(...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |