473,395 Members | 1,571 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,395 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 3859
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...
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,...

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.