How to display a decimal number to 2 digits without round off

Posted by Chaithragm under C# on 11/26/2013 | Points: 10 | Views : 828 | Status : [Member] | Replies : 6
i want to display a decimal numbers like

123.4567-->123.45 and not as 123.46
how to display the the decimal numbers without round off




Responses

Posted by: Allemahesh on: 11/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
See the below example and you will get your output.

string a = Convert.ToString("123.4567");

int i = a.IndexOf('.') + 3;
string d = a.Substring(0, i); // Here d = 123.45



Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chaithragm on: 11/26/2013 [Member] Starter | Points: 25

Up
0
Down
i want 123.45 as answer and not as 123.46
d=123.45 //i want this as the value of d

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
The above code will return the 123.45 and here once again code.

string a = Convert.ToString("123.4567");
int i = a.IndexOf('.') + 3;
string d = a.Substring(0, i); // Here d = 123.45




Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi use below code:
decimal i = 123.4567;
int j = i.ToString("N2");

N2 is used for displaying in 2 decimal places.

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Chaithragm,

You have got the solution. So
Can you click on MARK IT AS ANSWER link.

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/26/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Chaithragm,

Can you click on MARK IT AS ANSWER link.

Chaithragm, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response