Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
while i converted 0.1818181818181818 to string it shows me 0.181818181818182

the code is below
C#
double FinalResult = 0.1818181818181818;
TextBox1.Text = FinalResult.ToString();


in TextBox1.text it show me
VB
0.181818181818182

but i want exact value of varible FinalResult.
Posted
Updated 21-May-12 20:11pm
v2

Use this code:
C#
double FinalResult = 0.1818181818181818;
TextBox1.Text = FinalResult.ToString("R");
 
Share this answer
 
Comments
Anuja Pawar Indore 22-May-12 1:43am    
Perfect My 5!
Mohamed Mitwalli 22-May-12 2:18am    
+5
VJ Reddy 22-May-12 3:00am    
To the point 5!
Dear acceptProblem,

A double data type has a limit of 15 digits of max precision points, hence if you ever try to assign more than 15 digits, it rounds the assigned value at the time you fetch the data back. In your code you have 16 digits as precision (0.1818181818181818) hence it returns you rounded as (0.181818181818182). If you want to stick with the exact value then you need to use Decimal data type.

try the code below:

C#
decimal FinalResult = 0.1818181818181818M;
            TextBox1.Text = FinalResult.ToString();



This will fix your problem.

Happy Coding :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900