Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leave only two decimal places after the dot

public void LoadAveragePingTime() {     try     {         PingReply pingReply = pingClass.Send("logon.chronic-domination.com");         double AveragePing = (pingReply.RoundtripTime / 1.75);          label4.Text = (AveragePing.ToString() + "ms");                     }     catch (Exception)     {         label4.Text = "Server is currently offline.";     } } 

Currently my label4.Text get's something like: "187.371698712637".

I need it to show something like: "187.37"

Only two posts after the DOT. Can someone help me out?

like image 438
Sergio Tapia Avatar asked Aug 18 '09 02:08

Sergio Tapia


People also ask

How do you limit decimal places to 2?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How do I restrict to two decimal places in Excel?

Select the cells that you want to format. On the Home tab, click Increase Decimal or Decrease Decimal to show more or fewer digits after the decimal point.

How do I limit the number of decimal places?

Select the cell with a number (B2) and in the Ribbon, go to Home > Number Format. 2. In the Format Cells window, enter the number of decimal places (for example, 3) and click OK. You can immediately see how the number will look in the Sample box.

How do I show only 2 digits after a decimal in HTML?

parseFloat(num).toFixed(2);


2 Answers

string.Format is your friend.

String.Format("{0:0.00}", 123.4567);      // "123.46" 
like image 102
Matt Grande Avatar answered Sep 16 '22 11:09

Matt Grande


If you want to take just two numbers after comma you can use the Math Class that give you the round function for example :

float value = 92.197354542F; value = (float)System.Math.Round(value,2);         // value = 92.2; 

Hope this Help
Cheers

like image 35
Anas Avatar answered Sep 20 '22 11:09

Anas