도트 뒤에 소수점 이하 두 자리 만 남겨 두십시오
-
18-09-2019 - |
문제
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.";
}
}
현재 내 label4.text get은 다음과 같습니다. "187.371698712637".
"187.37"과 같은 것을 보여줄 필요가 있습니다.
점 후 두 개의 게시물 만 있습니다. 누군가 나를 도울 수 있습니까?
해결책
string.Format
당신의 친구입니다.
String.Format("{0:0.00}", 123.4567); // "123.46"
다른 팁
쉼표 이후에 두 번만 가져 가려면 예를 들어 라운드 기능을 제공하는 수학 클래스를 사용할 수 있습니다.
float value = 92.197354542F;
value = (float)System.Math.Round(value,2); // value = 92.2;
이 도움을 바랍니다
건배
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
http://www.csharp-examples.net/string-format-double/
편집하다
왜 그들이 "문자열"대신 "문자열"을 사용했는지 모르겠지만 나머지는 정확합니다.
double amount = 31.245678;
amount = Math.Floor(amount * 100) / 100;
당신은 이것을 사용할 수 있습니다
"string.format ("{0 : f2} ", 문자열 값);"
// give you only the two digit after Dot, excat two digit.
또는 복합 연산자 F를 사용한 다음 소수점 후에 나타나고 싶은 소수점이 있는지를 나타낼 수도 있습니다.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568
그것은 반올림 될 것이므로 그것을 알고 있어야합니다.
나는 일반 문서를 공급했다. 체크 아웃 할 수있는 다른 형식 연산자도 수많은 톤이 있습니다.
원천: https://msdn.microsoft.com/en-us/library/dwawy9k(v=vs.110).aspx
이 시도
public static string PreciseDecimalValue(double Value, int DigitsAfterDecimal)
{
string PreciseDecimalFormat = "{0:0.0}";
for (int count = 2; count <= DigitsAfterDecimal; count++)
{
PreciseDecimalFormat = PreciseDecimalFormat.Insert(PreciseDecimalFormat.LastIndexOf('}'), "0");
}
return String.Format(PreciseDecimalFormat, Value);
}
속성 사용 String
double value = 123.456789;
String.Format("{0:0.00}", value);
참고 : 이것은 표시하는 데 사용할 수 있습니다.
사용 System.Math
double value = 123.456789;
System.Math.Round(value, 2);
이 시도:
double result = Math.Round(24.576938593,2);
MessageBox.Show(result.ToString());
출력 : 24.57
간단한 해결책 :
double totalCost = 123.45678;
totalCost = Convert.ToDouble(String.Format("{0:0.00}", totalCost));
//output: 123.45
yourValue.ToString("0.00") will work.
문자열 보간을 사용하십시오 decimalVar:0.00
double doublVal = 123.45678;
두 가지 방법이 있습니다.
문자열 표시 :
String.Format("{0:0.00}", doublVal );
다시 두 배가됩니다
doublVal = Convert.ToDouble(String.Format("{0:0.00}", doublVal ));