473,511 Members | 15,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to maintain precision of fp number on string conversion?

I have the need to preserve the precision of a user-entered number, but that
needs to be stored in a (non-string) floating point variable (such as double or
decimal). Due to the internal limitations of numerical precision of the double
type, I'm presuming that the value will be stored in a decimal for the time
being.

Supposing the following:

decimal d1 = 123.45;
decimal d2 = 888.1234567;
decimal d3 = 677.100;
decimal d4 = 1.0;

Now, when converting to string, I want the following:

string s1 = d1.ToString(/*format???*/); // Should be "123.45"
string s2 = d2.ToString(/*format???*/); // Should be "888.1234567"
string s3 = d3.ToString(/*format???*/); // Can be "677.1"
string s4 = d4.ToString(/*format???*/); // Should be "1";

I don't want s1 to be something like:

"123.45000000"

and I don't want s2 to be truncated to something like:

"888.123"

Get what I'm after? Is there a format string that will handle this?

If not, what I currently have is to simply string.Trim("0") the trailing zeros
and then string.Trim(".") to trim a lagging decimal.
Nov 16 '05 #1
1 3869
Julie wrote:

I have the need to preserve the precision of a user-entered number, but that
needs to be stored in a (non-string) floating point variable (such as double or
decimal). Due to the internal limitations of numerical precision of the double
type, I'm presuming that the value will be stored in a decimal for the time
being.

Supposing the following:

decimal d1 = 123.45;
decimal d2 = 888.1234567;
decimal d3 = 677.100;
decimal d4 = 1.0;

Now, when converting to string, I want the following:

string s1 = d1.ToString(/*format???*/); // Should be "123.45"
string s2 = d2.ToString(/*format???*/); // Should be "888.1234567"
string s3 = d3.ToString(/*format???*/); // Can be "677.1"
string s4 = d4.ToString(/*format???*/); // Should be "1";

I don't want s1 to be something like:

"123.45000000"

and I don't want s2 to be truncated to something like:

"888.123"

Get what I'm after? Is there a format string that will handle this?

If not, what I currently have is to simply string.Trim("0") the trailing zeros
and then string.Trim(".") to trim a lagging decimal.


Found the answer myself. A format string of "G" looks like the solution, just
didn't see it first time around in the string formatting docs.
Nov 16 '05 #2

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

Similar topics

4
7829
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
2
2793
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
8
1797
by: Michel | last post by:
Hi there, I need to make a poisson distribution function that uses: double Math.Exp(double d) The d argument is a negative number in my case. When d becomes bigger and bigger, the result...
5
7974
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
2
1687
by: adrin | last post by:
hello, how can i make a conversion from a string to a multiple precision integer stored in memory in a base 2^32? i guess conversion from integer to string would require dividing modulo 10, but...
16
6200
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
5
2680
by: towers | last post by:
Hello, I've got a bit of experience in C++, but I'm writing my first app that is dependent on relatively precise math functions. The app requires that I get a time stamp based on s sample...
1
258
by: Julie | last post by:
I have the need to preserve the precision of a user-entered number, but that needs to be stored in a (non-string) floating point variable (such as double or decimal). Due to the internal...
6
17040
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
0
7148
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
7430
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...
1
7089
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...
1
5072
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...
0
4743
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...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
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 ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
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...

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.