Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Double to string conversion

Reply
Thread Tools

Double to string conversion

 
 
utab
Guest
Posts: n/a
 
      02-27-2006
Hi there,

Is there a way to convert a double value to a string. I know that there
is fcvt() but I think this function is not a part of the standard
library. I want sth from the standard if possible.

The thing I am trying to do is to convert a double value to a string
with 8 elements. 8 is fixed because of the files I work with. I will
change this 8 character string with the one(8 character string) already
in the file and so on. But I have to do the conversion first.

Any help will be appreciated,

Thanks in advance.

 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      02-27-2006
#include <sstream>
#include <iomanip>
#include <iostream>

using namespace std;


stringstream Text;
double Somevalue = 9.987654;

Text << Somevalue;

cout << Text.str() << endl;

// TODO: Lookup the iomanip stuff to get the format you want
--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"utab" <> wrote in message
news: oups.com...
> Hi there,
>
> Is there a way to convert a double value to a string. I know that there
> is fcvt() but I think this function is not a part of the standard
> library. I want sth from the standard if possible.
>
> The thing I am trying to do is to convert a double value to a string
> with 8 elements. 8 is fixed because of the files I work with. I will
> change this 8 character string with the one(8 character string) already
> in the file and so on. But I have to do the conversion first.
>
> Any help will be appreciated,
>
> Thanks in advance.
>



 
Reply With Quote
 
 
 
 
TB
Guest
Posts: n/a
 
      02-27-2006
utab skrev:
> Hi there,
>
> Is there a way to convert a double value to a string. I know that there
> is fcvt() but I think this function is not a part of the standard
> library. I want sth from the standard if possible.
>
> The thing I am trying to do is to convert a double value to a string
> with 8 elements. 8 is fixed because of the files I work with. I will
> change this 8 character string with the one(8 character string) already
> in the file and so on. But I have to do the conversion first.
>
> Any help will be appreciated,
>
> Thanks in advance.
>


#include <sstream>
#include <string>

template<typename T>
std::string AnyToString(T t) {
std:stringstream sstrm;
sstrm << t;
return sstrm.str();
}

--
TB @ SWEDEN
 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      02-27-2006
Thx for the quick reply Moonlit.

 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      02-27-2006
Thx TB

 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      02-27-2006
I want to raise a long integer to another long integer and have the
result as a long integer. Is there a nice way to do this, or do I have
to use this approach:
(long)pow((double)some_long_integer,(int)some_othe r_long_integer)
?
It seems very unnecessary and risky (in terms of potential data loss).

An unrelated problem is that when passing multidimensional arrays as
parameters to functions, it’s necessary to specify the depths of all but
the first dimension. The compiler says that these must be constant (not
variables), but I need to specify the depths using variables which are
declared constant and whose values are known at compile time. Is this
possible?

Martin
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      02-27-2006
Martin posted:

> I want to raise a long integer to another long integer and have the
> result as a long integer.


Why not just write a function. Potentially buggy code:


/***************************
Undefined Behaviour if
either argument is zero
***************************/

unsigned long RaisePowerInteger(unsigned long a, unsigned long b)
{
unsigned long value = a;

for ( unsigned long i = 0; i < b; ++i )
{
value *= a;
}

return value;
}
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      02-27-2006

Martin wrote:
> An unrelated problem is that when passing multidimensional arrays as
> parameters to functions, it's necessary to specify the depths of all but
> the first dimension. The compiler says that these must be constant (not
> variables), but I need to specify the depths using variables which are
> declared constant and whose values are known at compile time. Is this
> possible?


Yes, you need to use compile time constants but they need not be
constant literals. Can you post a minimal code example that is causing
you a problem.

Have you got a good reason to rule out a vector of vectors [of vectors
....] in preference to raw arrays?

Gavin Deane

 
Reply With Quote
 
Richard G. Riley
Guest
Posts: n/a
 
      02-27-2006
On 2006-02-27, Tomás <> wrote:
> Martin posted:
>
>> I want to raise a long integer to another long integer and have the
>> result as a long integer.

>
> Why not just write a function. Potentially buggy code:
>
>
> /***************************
> Undefined Behaviour if
> either argument is zero
> ***************************/
>
> unsigned long RaisePowerInteger(unsigned long a, unsigned long b)
> {
> unsigned long value = a;
>
> for ( unsigned long i = 0; i < b; ++i )
> {
> value *= a;
> }
>
> return value;
> }


Is this a new term of "undefined" that is specific to this language?

Certainly "defined" but incorrect because isnt any number raised to the power
of 0 equal to 1?

The function is defined in that if b is 0, the answer will be a. If a
is zero then the answer is 0.





--
Remove evomer to reply
 
Reply With Quote
 
Martin
Guest
Posts: n/a
 
      02-27-2006
Tomás wrote:
> Martin posted:
>
>
>>I want to raise a long integer to another long integer and have the
>>result as a long integer.

>
>
> Why not just write a function. Potentially buggy code:
>
>
> /***************************
> Undefined Behaviour if
> either argument is zero
> ***************************/
>
> unsigned long RaisePowerInteger(unsigned long a, unsigned long b)
> {
> unsigned long value = a;
>
> for ( unsigned long i = 0; i < b; ++i )
> {
> value *= a;
> }
>
> return value;
> }


Good idea, thanks!

Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion double* to vector<double> and vice versa J.M. C++ 5 03-08-2007 03:45 PM
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double -> C-String conversion Der Andere C++ 16 04-27-2004 02:31 PM
Conversion from double to string (from newbie) Marco Traverso C++ 5 12-07-2003 02:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57