473,781 Members | 2,732 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::pe rpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2 f",m);
Mar 29 '08 #1
2 2639
On Mar 28, 9:08 pm, yogi_bear_79 <yogi_bear...@y ahoo.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::pe rpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2 f",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::pe rpBisect(const double m, const Point p)
char M[6] ;
sprintf(M,"%2.2 f",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::stringstre am Stream;
Stream << Value;
std::string Digits;
Stream >Digits;

std::string::si ze_type Pos = Digits.find('.' );
std::string WholeNumber;
std::string Fraction;
if ( Pos == std::string::np os )
{
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*******@rocke tmail.com
Mar 29 '08 #3

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

Similar topics

4
47071
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
3
7773
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 DB and display it onscreen. No matter which way I open the file, convert it to Unicode/leave it as is or what ever, I see all single bytes ok, but double bytes become 2 seperate single bytes. Surely there is an easy way to convert these mixed...
1
1532
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
96526
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, JKop
17
4378
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 problem writing VB Double values to the file so as the Pascal program can read them as Pascal Real values. I've managed to find the algorithm to read the Pascal Real format and convert it to a VB Double, but I cannot figure out the opposite...
4
4524
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 it seems this convert is determined by the local settings and comma is indeed used as decimal separator. Is there another way to convert a dotted value to a double variable? Like 1234.5 and not 1234,5
3
8749
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; this produced an error
3
3856
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 new string value is being rounded to nine decimal places. Dim dblX As Double = 146785.62713610753 dim strText as String = dblX.ToString()
10
1863
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See section 4.7 for Rounding issues.
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.