Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trincate to one decimal place
Message
De
15/06/2010 00:37:59
 
 
À
14/06/2010 12:55:08
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01468807
Message ID:
01468911
Vues:
76
>>>In .NET you can use math.truncate(number*10)/10
>>>
>>>Thanks - I should have thought of that myself.
>>
>>Why the truncate function doesn't allow you to truncate at a certain position is one of those 'what were they thinking when they designed it' questions..
>
>Maybe a simple extension method. Something like :
public static decimal Trunc(this decimal d, int digits)
>        {
>            decimal mult = (decimal) Math.Pow(10, digits);
>            return Math.Truncate(d * mult) / mult;
>        }
Then:
Decimal d = 1.2344567M;
>var s = d.Trunc(4);
Another flavour
		public static decimal Trunc(this decimal d, int digits)
		{
			return d - (d % (decimal)Math.Pow(10, -digits));
		}
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform