PC Review


Reply
Thread Tools Rate Thread

Creating the Double2 type

 
 
FrankVDL@gmail.com
Guest
Posts: n/a
 
      2nd Sep 2005
Hi,

I have some problems with the default rounding method of C#

check an earlier post of my :
http://groups.google.be/group/micros...10c5e788c35f06

To create a workaround for this problem, I wan't to create my own
double type !

the 'Double2' would act like a standard double but when assinging a
value to it, it will always my own rounding method to round the value
to 2 decimals.

Double2 d = 1.235;

d -> 1.23;

How can I create my own type like this ?

Kind regards
Frank Vandelinden

 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      2nd Sep 2005
>How can I create my own type like this ?

Create a struct with implicit conversion operators to/from double.

But is there any reason you're not using the Decimal type?


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Bruno Jouhier
Guest
Posts: n/a
 
      3rd Sep 2005
Also, stop thinking "double", think "decimal". If you don't see why try the
following:

Console.WriteLine("double test: " + (0.1d + 0.2d == 0.3d));
Console.WriteLine("decimal test: " + (0.1m + 0.2m == 0.3m));

And please, don't respond that this must be a bug in the way .NET implements
doubles, this is inherent to base 2 floating point arithmetics.

Bruno.

"Bruno Jouhier" <(E-Mail Removed)> a écrit dans le message de news:
4319668f$0$21299$(E-Mail Removed)...
> First, don't call it Double2 because
> * "double" is already a bad name for double precision floating point
> * your type will be a fixed-point decimal type, not a floating point base
> 2 type.
> So, Decimal2 would probably be a better name.
>
> Then, before embarking into the implementation of a new type, investigate
> if you could not use the standard decimal type, maybe with some static
> helper methods to perform the rounding operations that you need.
>
> If you really decide that you need a custom type, follow the other
> poster's advice, implement it as a struct. You can use a long for the
> value (your decimal value scaled by a factor of 100).
>
> And I recommend that you get good documentation on floating-point, fixed
> point arithmetics, the .NET decimal type and legal recommendations for
> rounding before you embark into implementing a new type.
> For an in-depth understanding of floating point types, my favorite page is
> http://docs.sun.com/source/806-3568/ncg_goldberg.html
> The .NET decimal type is documented in the on-line doc of Visual Studio.
> So, you don't have any excuse to not read it.
>
> Bruno
>
> <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>> Hi,
>>
>> I have some problems with the default rounding method of C#
>>
>> check an earlier post of my :
>> http://groups.google.be/group/micros...10c5e788c35f06
>>
>> To create a workaround for this problem, I wan't to create my own
>> double type !
>>
>> the 'Double2' would act like a standard double but when assinging a
>> value to it, it will always my own rounding method to round the value
>> to 2 decimals.
>>
>> Double2 d = 1.235;
>>
>> d -> 1.23;
>>
>> How can I create my own type like this ?
>>
>> Kind regards
>> Frank Vandelinden
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating List<Type> from type name Andrus Microsoft C# .NET 1 21st Jan 2008 04:55 PM
Creating T From Type sternr Microsoft C# .NET 4 13th Mar 2007 10:48 AM
creating a value type by reflection farseer Microsoft VB .NET 6 20th Oct 2005 03:31 PM
creating a value type by reflection farseer Microsoft C# .NET 1 19th Oct 2005 09:00 AM
Cast from type 'DBNull' to type 'String' is not valid. when creating dataset troutbum Microsoft ADO .NET 5 13th Mar 2004 02:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 AM.