How to maintain precision of fp number on string conversion?

J

Julie

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.
 
J

Julie

Julie said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top