Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3610276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:43:55+00:00 2026-05-18T21:43:55+00:00

Is there a way in .NET to create a type derived from decimal that

  • 0

Is there a way in .NET to create a type derived from decimal that would be used as a currency, so it rounds the arithmetic operations to the desired number of decimal points.

If not, what are the best practice for it in .NET?

EDIT (motivation):

Let’s say I have a price:

125.00 Rep$

and I sell 0.25 pieces of it, that amounts to 31.25

Now, I have a discount of 15%, and to calculate discount and present it in absolute value I’ll have:

31.25 * .85 = 26.5625

If I use other way:

31.25 * .15 = 4.6875

If I let some 3rd party control to truncate it and display it, for example, I could have:

26.56 +
 4.68 =
-----
31.24

And every accountant will eat your hearth if you give her something like that.

Add tax here, and problem multiplies further.

So, NO storing as much decimals and rounding it as late as possible. Create a class that will do it financially correct and store it as soon as possible rounded/truncated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:43:56+00:00Added an answer on May 18, 2026 at 9:43 pm

    You certainly can’t create a type derived from decimal, as it’s a struct – and you can’t create subtypes of structs.

    You could create your own Currency struct which contained a decimal, though. You’d want to overload all the arithmetic operators to basically perform the arithmetic on the contained decimal values and then round appropriately.

    For example:

    public struct Currency
    {
        private readonly decimal value;
    
        public Currency(decimal value)
        {
            this.value = decimal.Round(value, 2);
        }
    
        public override string ToString()
        {
            return value.ToString();
        }
    
        public static Currency operator+(Currency left, Currency right)
        {
            return new Currency(left.value + right.value);
        }
    
        public static Currency operator-(Currency left, Currency right)
        {
            return new Currency(left.value - right.value);
        }
    
        public static Currency operator/(Currency left, int right)
        {
            return new Currency(left.value / right);
        }
    }
    
    class Test
    {
        static void Main()
        {
            Currency currency = new Currency(15);
            Console.WriteLine(currency / 10); // Prints 1.5
            Console.WriteLine(currency / 100); // Prints 0.15
            Console.WriteLine(currency / 1000); // Prints 0.2
        }
    }
    

    (Obviously there’s a lot more needed here – in particular you’ll want to override GetHashCode and Equals, implement IEquatable<T> etc.)

    Is this definitely what you want though? I thought it was more common to keep as much precision as you could during intermediate operations, and only round at the last moment, e.g. for storage in a database or display to a user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.