473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Double to string issues

I have a double of unknown length that I need to split at the
decimal. I thought I would convert it either to a string or a char.
char seems to be the best since it easily lends itself to splitting.
I have a few issues with this theory. Hard codeing char M[6] works
fine for my test double -0.5 But I would like it to be more
versitile. The format "%2.2f" works fine with my test data, but that
should also be versitile.
inline void LineSegment::perpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2f",m);
Mar 29 '08 #1
2 2612
On Mar 28, 9:08 pm, yogi_bear_79 <yogi_bear...@yahoo.comwrote:
I have a double of unknown length that I need to split at the
decimal. I thought I would convert it either to a string or a char.
char seems to be the best since it easily lends itself to splitting.
I have a few issues with this theory. Hard codeing char M[6] works
fine for my test double -0.5 But I would like it to be more
versitile. The format "%2.2f" works fine with my test data, but that
should also be versitile.

inline void LineSegment::perpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2f",m);
you can use stringstream for converting double to string. To find the
digits after decimal, you may then use the string functions(find and
substr).

Thanks,
Balaji.
Mar 29 '08 #2
yogi_bear_79 wrote:
I have a double of unknown length that I need to split at the
decimal. I thought I would convert it either to a string or a char.
char seems to be the best since it easily lends itself to splitting.
I have a few issues with this theory. Hard codeing char M[6] works
fine for my test double -0.5 But I would like it to be more
versitile. The format "%2.2f" works fine with my test data, but that
should also be versitile.
inline void LineSegment::perpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2f",m);
Here is a way using stringstream. Depending on what you need to do with the
parts depends on how you would optimize it if necessary.

Output is:
Whole Number: 1 Fraction: 2345
Whole Number: 2 Fraction: 0
Whole Number: 0 Fraction: 333333

#include <string>
#include <sstream>
#include <iostream>

void ShowDouble( const double& Value )
{
std::stringstream Stream;
Stream << Value;
std::string Digits;
Stream >Digits;

std::string::size_type Pos = Digits.find('.');
std::string WholeNumber;
std::string Fraction;
if ( Pos == std::string::npos )
{
WholeNumber = Digits;
Fraction = "0";
}
else
{
WholeNumber = Digits.substr( 0, Pos );
Fraction = Digits.substr( Pos + 1 );
}

std::cout << "Whole Number: " << WholeNumber << " Fraction: " <<
Fraction << "\n";
}

int main()
{
ShowDouble( 1.2345 );
ShowDouble( 2 );
ShowDouble( 1.0 / 3 );
}
--
Jim Langston
ta*******@rocketmail.com
Mar 29 '08 #3

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

Similar topics

4
47048
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
3
7723
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
1
1518
by: lbzheng | last post by:
I want to convert a string(example "3.1415") to double. Are there function do it in c/c++ library ? thanks, lokicer
6
96503
by: JKop | last post by:
What's the best method to use? I've searched the web a bit and all I've come up with is "sprintf". Also, for the converse, ie. string to double, is "atof" the way to go? Kind Regards, ...
17
4342
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
4
4502
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
3
8723
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
3
3828
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, I would like to convert a double to a string and have the resulting string contain all of the original values after the decimal place. Currently, the problem that I am having is that the...
10
1829
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
7216
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
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,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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
5613
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,...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.