473,587 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion of double value to string

1 New Member
Hi Everyone,
I have a double value (e.g. 2.00) which I would like to convert to string. Simply returning the value using the .ToString() method trims the 0s after the decimal point. I know that its is possible by doing the following
double d1 = 2.00;
string strValue;
strValue = d1.ToString("#, ###.00")

However the problem is that I do not know how many 0s there will be after the decimal point before hand. The number could be 2.0 or 2.00 or 2.000 but I need to convert this value to string value exactly as it is without trimming the 0s. If anybody's got a solution to this do let me know.

Thanks in advance.
Apr 4 '07 #1
4 1525
nmadct
83 Recognized Expert New Member
I think I've run into the same problem before.

How's this?

strValue = d1.ToString("#, ###." + new String('0', numZeros));
Apr 4 '07 #2
nmadct
83 Recognized Expert New Member
I might have misunderstood the question. You seem to mean that your stored double value is 2.00, and because the double value has 2 trailing zeros, the output should preserve them as well. The problem is that your double value does NOT have 2 trailing zeros: the internal representation of a double has no concept of preserving trailing digits, or a degree of precision. You will have to keep track of this separately.
Apr 4 '07 #3
SammyB
807 Recognized Expert Contributor
Hi Everyone,
I have a double value (e.g. 2.00) which I would like to convert to string. Simply returning the value using the .ToString() method trims the 0s after the decimal point. I know that its is possible by doing the following
double d1 = 2.00;
string strValue;
strValue = d1.ToString("#, ###.00")

However the problem is that I do not know how many 0s there will be after the decimal point before hand. The number could be 2.0 or 2.00 or 2.000 but I need to convert this value to string value exactly as it is without trimming the 0s. If anybody's got a solution to this do let me know.

Thanks in advance.
Use Strings.FormatN umber
Apr 4 '07 #4
ruckus
8 New Member
FormatNumber is your best bet, but you can also use Convert.ToStrin g or (your using VB right?) cstr. But yeah, he's right. Doubles do not store meaningless decimal points. You have to format them to be that way (usually) using FormatNumber, FormatCurrency, FormatPercent, or any other 'Format' command I forgot about.
Apr 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

31
6603
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
11
7603
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the...
15
2412
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: "pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan= BU:shp_Term=2"
21
4170
by: utab | last post by:
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...
14
2214
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit conversion" for a cast operation
5
2295
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and internally convert it so that it could be saved in database as real number (float). I plan to provide property(set) interface for reading the value.
14
8900
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) { istringstream(s); T val; is >val;
6
1817
by: Grey Alien | last post by:
I am baffled by this behaviour. I have a class A declared as follows: class A { public: A(); explicit A(const std::string& value); explicit A(const TimestampParam& value) ;
14
2155
by: Aman JIANG | last post by:
hi i need a fast way to do lots of conversion that between string and numerical value(integer, float, double...), and boost::lexical_cast is useless, because it runs for a long time, (about 60 times slower than corresponding C functions) it's too expensive for my program. is there any way( library?) to do this fast and safely, please ?
1
3499
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php ( http://scripts.ringsworld.com/database-tools/xls2mysql/inc/Excel/reader.php.html ) Somewhere there happens a exponent to float conversion so that...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
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. ...
1
7967
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...
0
8220
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...
0
6619
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...
1
5712
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
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...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
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...

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.