473,386 Members | 1,810 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,386 software developers and data experts.

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 3857
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.