Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
2.00/5 (5 votes)
See more:
HI
i have double variable as like

double abc=23.22

i want to convert it 4 decimal place as like abc=23.2200 without converting it into string formt or string.format() or .tostring("0.0000")

any one please help me.
Posted
Comments
Rajesh Anuhya 3-May-11 2:39am    
may i know why are you not using string.format??
CodeHawkz 3-May-11 3:48am    
I also would like to know the answer to Rajesh's question and to one more question.

You talk about converting, is it to another data type? or is it just that you want to format it so that you could display it? or is it just that your calculation precision is 4 decimal points?

On a side note, double abc = 23.22d; sets abc to 23.22000000 so it won't affect your calculations at all, the only place where it would affect is while displaying. Please, correct me I am wrong and give us an example of such an instance :)

Here is your Solution

C#
double a = 23.22;
           string msg = string.Format("E format: {0:N4}", a);
           MessageBox.Show(msg);
 
Share this answer
 
Comments
Ganesh_verma 3-May-11 3:40am    
aby ghadhay kay awlad ..........firt you read complete sentence.

after that give answer
Ankit Rajput 3-May-11 3:48am    
If you think answer is not up to the mark then you can downvote it. but do not use abusive language
Rajesh Anuhya 3-May-11 3:56am    
IDIOT.., mind your language.., you don't have a knowledge to solve your problem. And your question is a fault question(don't use that/this), that is not exactly happen in real time. Here we are breaking our heads to solve the issue.
Ganesh_verma 6-May-11 13:14pm    
Hello puli (actually u r not in the level of this question so that u breaking your head........this is very simple question) how much exp you have............i know very cleary this is not real time question ...........but we can made it real time........and i have done this.

Ankit Rajput 3-May-11 3:46am    
@Ganesh: do not use this type of language here.
He is trying to help you without any charge. Mind it.
decimal d=convert.todecimal(yourinput);
outputcontrol=string.format("{0:f2}",d);
 
Share this answer
 
Comments
Kim Togo 3-May-11 7:30am    
Good answer, but change "f2" to "f4".
SQL
Use   Math.Round(Decimal,   Integer)
sample:
Math.Round(23.22,   4)
result: 23.2200


VB
decimal.Round((decimal)23.12003, 4)
 - 23.1200
decimal.Round((decimal)23.120, 4)
 - 23.12
 
Share this answer
 
v2
Comments
Toniyo Jackson 3-May-11 2:38am    
This will not work
Ganesh_verma 6-May-11 13:20pm    
Tino this is working fine....
Rajesh Anuhya 3-May-11 3:37am    
@loveyou

Finally what do you want to say??.., above solution is not work ..
See my 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