2010-02-10 4 views

답변

15

Math.Round(val*20)/20

round 0.05

+0

이 조각은 사용'Math.Floor (발 * 20)/20' – Timeless

+1

에 관계없이 질문이 요청 된 횟수의 , 당신은 그런 사람을 결코 멀리해서는 안됩니다. 내가 downvoted. – Krythic

2

를 참조하십시오

전에 여러 번 요청되었습니다 여기에 방법의 몇 내가 그 항상 반올림 또는 값까지 것이다 썼다이다.

 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

에만 라운드까지 가장 가까운 0.05 다운 라운드

public static decimal Round(decimal value) { 
     var ceiling = Math.Ceiling(value * 20); 
     if (ceiling == 0) { 
      return 0; 
     } 
     return ceiling/20; 
    } 
관련 문제