hey les gars, comment arrondir une décimale à 0,05 cents près en C#?Arrondir un nombre décimal au mille près?
$ à 3,44 par exemple être arrondi à 3,45 $ ou 3,48 $ à 3,50
$ J'ai joué avec Math.round() si havent compris cela.
hey les gars, comment arrondir une décimale à 0,05 cents près en C#?Arrondir un nombre décimal au mille près?
$ à 3,44 par exemple être arrondi à 3,45 $ ou 3,48 $ à 3,50
$ J'ai joué avec Math.round() si havent compris cela.
Voici quelques méthodes que j'écrit cela arrondissez ou vers le bas à une valeur.
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;
}
}
Cet extrait ne rond jusqu'à la 0,05
public static decimal Round(decimal value) {
var ceiling = Math.Ceiling(value * 20);
if (ceiling == 0) {
return 0;
}
return ceiling/20;
}
pour le bas rond, utilisez 'Math.Floor (val * 20)/20' – Timeless
le plus proche Peu importe combien de fois une question a été posée , vous ne devriez jamais aliéner quelqu'un comme ça. J'ai downvoted. – Krythic