473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

CString to double?????

I am trying to convert a CString to a double using
atof()

however my nice CString of "0.58" gets converted to 0.579999223213147 etc

making it impossible to compare with a different double varaible of 0.58

Any suggestions on how to keep my origional value!!!!

Thanks

Ducnan
Nov 16 '05 #1
3 3006
TGF
Ducnan:

Usually whenever you compare two different doubles/floats, you should use a
tolercance value (according your problem-set needs). That is, if both
doubles are within some tolerance of each other, for this problem, say 0.001
or 0.0001, then they are considered equivalent. This helps eliminate the
problems with rounding like you are seeing when you convert using atof().
So here would be a function to do this......

if (double_equiv(0.58, atof(arg), 0.001)) {
//THEY ARE EQUAL
}

bool double_equiv(double val1, double val2, double tolerance)
{
bool equiv=false;

if (fabs(val1 - val2)<=tolerance) {
equiv = true;
}
return equiv;
}

...Hope this helps....

-TGF

"Duncan Winn" <te**@test1.co.uk> wrote in message
news:e9*************@tk2msftngp13.phx.gbl...
I am trying to convert a CString to a double using
atof()

however my nice CString of "0.58" gets converted to 0.579999223213147 etc

making it impossible to compare with a different double varaible of 0.58

Any suggestions on how to keep my origional value!!!!

Thanks

Ducnan

Nov 16 '05 #2
Duncan Winn wrote:
I am trying to convert a CString to a double using
atof()

however my nice CString of "0.58" gets converted to 0.579999223213147 etc

making it impossible to compare with a different double varaible of 0.58

Any suggestions on how to keep my origional value!!!!

Thanks

Ducnan


It is not possible to keep your original value. Many numbers cannot be
represented exactly in binary floating point. Just like 1/3 cannot be
represented exactly in the decimal number system. The accuracy is very
good, but you must understand and deal with this awkward phenomenon in
every step.

--
Scott McPhillips [VC++ MVP]

Nov 16 '05 #3
If "0.58" represents £0.58, you might consider using something like
System.Decimal instead of a double: "The Decimal value type is appropriate
for financial calculations requiring large numbers of significant integral
and fractional digits and no round-off errors."

Dan

"Duncan Winn" <te**@test1.co.uk> wrote in message
news:e9*************@tk2msftngp13.phx.gbl...
I am trying to convert a CString to a double using
atof()

however my nice CString of "0.58" gets converted to 0.579999223213147 etc

making it impossible to compare with a different double varaible of 0.58

Any suggestions on how to keep my origional value!!!!

Thanks

Ducnan

Nov 16 '05 #4

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

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
4
by: Susan Rice | last post by:
I'm new to using CString. Why won't the following compile? I'm using Microsoft Visual C++ 6.0 Line 37 which it complains about is the function: 37 CString ConvertFile(char *szFileName) I...
2
by: flyingxu | last post by:
Hi, I run into a cstring related link problem in VC7. My solution has 3 projects, one MFC exe, two MFC extersion DLL. the two MFC extersion DLL export functions which use CString as parameters....
10
by: mecho | last post by:
Hi, I want to excuse myself if my question may be lame, I am still a student, So I am making a calculator and the problem appears when a float value is entered in the dialog box (as CString) with...
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
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...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.