안녕 얘들 아, C에서 가장 가까운 0.05 센트로 소수점을 어떻게 써야합니까? havent 한이 알아 냈하지만 예 : $ 3.44십진수를 가장 가까운 0.05로 반올림 하시겠습니까?
은 $ 3.45 또는 내가 math.round와 함께 놀았 $ 3.48에서 $ 3.50
()로 반올림합니다.
안녕 얘들 아, C에서 가장 가까운 0.05 센트로 소수점을 어떻게 써야합니까? havent 한이 알아 냈하지만 예 : $ 3.44십진수를 가장 가까운 0.05로 반올림 하시겠습니까?
은 $ 3.45 또는 내가 math.round와 함께 놀았 $ 3.48에서 $ 3.50
()로 반올림합니다.
이 Math.Round(val*20)/20
를 참조하십시오
전에 여러 번 요청되었습니다 여기에 방법의 몇 내가 그 항상 반올림 또는 값까지 것이다 썼다이다.
public static Double RoundUpToNearest(Double passednumber, Double roundto)
{
// 105.5 up to nearest 1 = 106
// 105.5 up to nearest 10 = 110
// 105.5 up to nearest 7 = 112
// 105.5 up to nearest 100 = 200
// 105.5 up to nearest 0.2 = 105.6
// 105.5 up to nearest 0.3 = 105.6
//if no rounto then just pass original number back
if (roundto == 0)
{
return passednumber;
}
else
{
return Math.Ceiling(passednumber/roundto) * roundto;
}
}
public static Double RoundDownToNearest(Double passednumber, Double roundto)
{
// 105.5 down to nearest 1 = 105
// 105.5 down to nearest 10 = 100
// 105.5 down to nearest 7 = 105
// 105.5 down to nearest 100 = 100
// 105.5 down to nearest 0.2 = 105.4
// 105.5 down to nearest 0.3 = 105.3
//if no rounto then just pass original number back
if (roundto == 0)
{
return passednumber;
}
else
{
return Math.Floor(passednumber/roundto) * roundto;
}
}
에만 라운드까지 가장 가까운 0.05 다운 라운드
public static decimal Round(decimal value) {
var ceiling = Math.Ceiling(value * 20);
if (ceiling == 0) {
return 0;
}
return ceiling/20;
}
이 조각은 사용'Math.Floor (발 * 20)/20' – Timeless
에 관계없이 질문이 요청 된 횟수의 , 당신은 그런 사람을 결코 멀리해서는 안됩니다. 내가 downvoted. – Krythic