Solved

Working with decimals in C#

Posted on 2004-10-07
1
1,797 Views
Last Modified: 2010-05-18
I have a few questions regarding variables of type decimal in C#:

   1. How do I define a decimal literal? (i.e. 100.1)
 
   2. How do I compare a decimal variable to a literal?

       if (value > 100.1)             ..........is giving me an error about using the > with a decimal
       {
       }

Thanks,
Rosanne
0
Comment
Question by:RosanneParnell
1 Comment
 
LVL 2

Accepted Solution

by:
davidastle earned 50 total points
ID: 12252938
1.  Like the "f" suffix for floats, use the "M" suffix for decimals.
For example, 100.1M

2. You can compare a decimal variable to other decimal literals.
For example

if (value > 100.1M)
{
}

Alternatively, you can type cast 100.1 to a decimal (although this is not preferred, it is a good way to compare decimals to variables of other types).

if (value > (decimal)100.1)
{
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question