Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data issue 1

Status
Not open for further replies.

sand133

Programmer
Jun 26, 2004
103
GB
Hi I am trying to convert the following value of type double 0.000001096492799312173 to string, the problem is that it is removing the last digit, "3". i.e as a result I get 0.00000109649279931217.

please Help any ideas?
 
The best advice I can give you is, DON'T USE A DOUBLE if precision is important us non-floatingpoint things.

Christiaan Baes
Belgium

My Blog
 
Could you give me an example becuase precision is very important
 
Use decimal

MSDN said:
The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors.

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value is of the form, ((-296 to 296) / 10(0 to 28)), where -296-1 is equal to MinValue, and 296-1 is equal to MaxValue.

This type provides methods that convert Decimal values to and from type Char, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, and UInt64. Conversions from other types to Decimal are widening conversions that never lose information or throw exceptions.

Conversions from Decimal to other types are narrowing conversions that round the Decimal value to the nearest integer value toward zero. If the result of the conversion is not representable in the destination type, an OverflowException is thrown.

This type provides methods that convert Decimal values to and from Single and Double. Conversions from Decimal to Single or Double are narrowing conversions that might lose precision but not information about the magnitude of the converted value. The conversion will not throw an exception.

Conversions from Single or Double to Decimal throw an OverflowException if the result of the conversion is not representable as a Decimal.

This type implements interfaces IComparable, IComparable, IFormattable, and IConvertible. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.


There are other but I forget.


Christiaan Baes
Belgium

My Blog
 
You could also do this, assuming you know exactly how many decimals the value needs to be:

double number1 = 0.000001096492799312173;
string result = number1.Value.ToString("F26");

The "F26" attribute is stating that the string will have 26 decimal places. Hope that helps.
 
Yes but you can never be sure what value the double has since it's a floating point and an aproximation of the value not the real thing.



Christiaan Baes
Belgium

My Blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top