Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Float or double to string....

Posted on 2002-05-05
11
238 Views
Last Modified: 2010-04-02
Hello Experts...

There is a library for converting integers to chars.
itoa....
But I didn't find any to do the same with noninteger numbers...
For instances... How can I convert the number 12.365 to a char array with "12.365"?.

If there is no function that Does that... I guess I will must write one....
How can I handle a double var?
I mean.... Is it right to say that the first four bytes contains the int part of the number and the last four bytes the decimal part?...

I need some help about this....

Thank you...
0
Comment
Question by:Marsc
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6990611
use either

/* _GCVT.C: This program converts -3.1415e5
 * to its string representation.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char buffer[50];
   double source = -3.1415e5;
   _gcvt( source, 7, buffer );
   printf( "source: %f  buffer: '%s'\n", source, buffer );
   _gcvt( source, 7, buffer );
   printf( "source: %e  buffer: '%s'\n", source, buffer );
}


or

#include <sstream>

stringstream ss;
double d = 3.141;

ss << d;

const char* psz = ss.str().c_str();
0
 
LVL 86

Expert Comment

by:jkr
ID: 6990613
Ooops, correction - the latter sample should read

#include <sstream>
using namespace std;

stringstream ss;
double d = 3.141;

ss << d;

const char* psz = ss.str().c_str();
0
 
LVL 1

Accepted Solution

by:
Shivshankar earned 100 total points
ID: 6990993
I'd suggest you rather use sprintf.

sprintf(str, "%5.2f", some_float_number);

The simplest solution that I always use. More importantly I get it in whatever format I want. :-)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:Marsc
ID: 6991172
I cannot use printf... The program runs under Windows....
So.... I need the string to use the API DrawText ()....
I will see what is _gcvt about....

0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6991245
> I cannot use printf... The program runs under Windows....
>So.... I need the string to use the API DrawText ()....

sprintf works fine under windows. Just sprintf to a buffer and use DrawText on that buffer.

char str[100];

sprintf(str, "%5.2f", some_float_number);

DrawText(hDC,str,strlen(str),rect,uFormat);

// see the doc for DrawText for a description of the params.



Are you using MFC. If so use the following;

//cdc is a CDC for the window you need to draw on.

CString str;

str.Format("%5.2f", some_float_number);

cdc.DrawText(str,rect,nFormat);



John
0
 
LVL 1

Expert Comment

by:Yechezkel
ID: 6993412
0
 
LVL 1

Author Comment

by:Marsc
ID: 7014651
All anwsers were good, but Shivshankar's was the first...

Thank you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7014681
>>but Shivshankar's was the first...

Accepted Answer
From: Shivshankar  Date: 05/06/2002 02:47AM PST  

Comment
From: jkr  Date: 05/05/2002 02:51PM PST  

That is a whole *day* later...
0
 
LVL 1

Author Comment

by:Marsc
ID: 7016376
jkr:

I am sorry about this.... You are right...
I always try to be fair but I mistaken about this...
Let me know if you want me to post some question with points for you.
0
 
LVL 1

Expert Comment

by:Shivshankar
ID: 7019660
Yes jkr,

I'm a day later than you.
But, I guess, its a simple and straightforward solution.

Best Regards.
0
 
LVL 1

Author Comment

by:Marsc
ID: 7022613
Shivshankar

If we talk about to be fair...
Actually you are right

:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 131
C Language combined operators 28 114
How to copy an image file into clipboard C/C++? 1 197
Dynamically allocate memory 9 62
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question