Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trincate to one decimal place
Message
From
15/06/2010 03:02:43
 
 
To
15/06/2010 00:37:59
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01468807
Message ID:
01468926
Views:
50
>>>>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));
>		}
>
Neater.....
Previous
Reply
Map
View

Click here to load this message in the networking platform