Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,,


I need to round a decimal value

Ex : 2.365 become 2.360
3.239 become 3.230



thanks

Afsal
Posted
Comments
Tejas Vaishnav 11-Mar-14 4:45am    
you are not rounding in your ex.. like if value is 3.239 then it will become like 3.240 not 3.230...
you are trying to round last digit to 0 or you need to round your decimal to nearest number?
afsal.mp 11-Mar-14 4:57am    
yes... i nee 3.239 become 3.230
Kenneth Haugland 11-Mar-14 5:11am    
You should use the truncate function:
http://msdn.microsoft.com/en-us/library/c2eabd70(v=vs.110).aspx
george4986 11-Mar-14 5:29am    
decimal value of 2.360 is 2.36 is it necessary to add zero?

try this
C#
double inVal = 2.365;
 double outVal = Math.Truncate(inVal * 100) / 100;
 string outStr = outVal.ToString("#.000");

decimal value of 2.360 is 2.36
outVal ->2.36
outStr ->2.360

[Agent_Spock]

or you can do it like this
C#
decimal value = Math.Round(Convert.ToDecimal("12.395"), 2);
string outStr  = value.ToString("#.000");

value is 12.4
outStr is 12.400
 
Share this answer
 
v2
Comments
george4986 11-Mar-14 6:58am    
agent_spock ,its not the requirement
12.395->12.390 NOT 12.400
afsal.mp 11-Mar-14 7:13am    
Thank you very much
george4986 11-Mar-14 7:16am    
happy to hear it works
gud luck ;-)
System.Math.Round(x,0)
 
Share this answer
 
Comments
afsal.mp 11-Mar-14 4:56am    
it will get only first value.. it will not get after decimal point
try this.. :)

C#
double d = 1.275;
Math.Round(d, 2);          // 1.27
Math.Round((decimal)d, 2); // 1.28 
 
Share this answer
 
v2
Comments
afsal.mp 11-Mar-14 4:56am    
i need c# code
Nirav Prabtani 11-Mar-14 6:04am    
try updated solution
ArunAmalraj 11-Mar-14 5:07am    
System.Math.Round(x,0)

Replace 0 by the 'n' - digits after the decimal point

Ex: System.Math.Round(x,3) gives you x.450

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