2010-05-13 35 views

risposta

49

moltiplicarlo per quattro, round come è necessario un intero, poi dividerlo per quattro di nuovo:

x = Math.Round (x * 4, MidpointRounding.ToEven)/4; 

Le varie opzioni per l'arrotondamento, e le loro spiegazioni, si possono trovare in questo eccellente risposta here :-)

6

in alternativa, è possibile utilizzare UltimateRoundingFunction dato in questo blog: http://rajputyh.blogspot.in/2014/09/the-ultimate-rounding-function.html

//amountToRound => input amount 
//nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1 
//fairness => btween 0 to 0.9999999___. 
//   0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling 
//   0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1. 
//   0.4999999... non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2. 
//   0.75 means first 75% values will be rounded down, rest 25% value will be rounded up. 
decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness) 
{ 
    return Math.Floor(amountToRound/nearstOf + fairness) * nearstOf; 
} 

Chiama sotto per arrotondamento standard. cioè 1.125 viene arrotondato a 1,25

UltimateRoundingFunction(amountToRound, 0.25m, 0.5m); 

chiamata sotto per arrotondando valori di confine. vale a dire 1.125 sarà arrotondato a 1,00

UltimateRoundingFunction(amountToRound, 0.25m, 0.4999999999999999m); 

cosiddetto "banchiere arrotondamento" non è possibile con UltimateRoundingFunction, si è di andare con la risposta di paxdiablo per tale sostegno :)

Problemi correlati