473,748 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable type Money/Currency

What is the best way to handle money in C#?

Do you use float type?

Then how do you handle rounding problems with cents as well as how best to
display it?

Thanks,

Tom
Nov 17 '05 #1
11 14980
You'd probably be better off using a Decimal type

Nov 17 '05 #2
tshad,

For money, I would use the Decimal type (decimal is the alias in C#).
It is guaranteed to be precise (up to a certain number of places, enough for
financial calcs, IMO). However, the downfall is that using it is slower, so
you have to pick your poison.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"tshad" <ts**********@f tsolutions.com> wrote in message
news:ek******** ******@TK2MSFTN GP11.phx.gbl...
What is the best way to handle money in C#?

Do you use float type?

Then how do you handle rounding problems with cents as well as how best to
display it?

Thanks,

Tom

Nov 17 '05 #3
Hi,

The bigger precision is by using Decimal.
To display it friendly you could use Decimal.ToStrin g( "C" );

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"tshad" <ts**********@f tsolutions.com> wrote in message
news:ek******** ******@TK2MSFTN GP11.phx.gbl...
What is the best way to handle money in C#?

Do you use float type?

Then how do you handle rounding problems with cents as well as how best to
display it?

Thanks,

Tom

Nov 17 '05 #4
Tshad,

The best you can do with money is sent it to me, I can use a lot.

However be aware that in the version 2005 the rounding has more
possibilities. Because at the moment is all going by bankings rounding,
which is in my opinion rarely used.

For the rest see Chris his message.

Cor
What is the best way to handle money in C#?

Do you use float type?

Then how do you handle rounding problems with cents as well as how best to
display it?

Thanks,

Tom

Nov 17 '05 #5
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
You'd probably be better off using a Decimal type
But how do you add cents?

I tried doing:

deductionTotals += 100.15

and got 2 errors:

Literal of type double cannot be implicitly converted to type 'decimal';
use an 'M' suffix to create a literal of this type

and

Operator '+=' cannot be applied to operands of type 'decimal' and
'double'

Do I need to take all my calculations and multiply them by 100?

Thanks,

Tom

Nov 17 '05 #6
The help for CS0664 is pretty clear. By default, doubles cannot be
implicitly converted to decimal. 100.15 is considered a double. To
make it decimal write 100.15M:

deductionTotals += 100.15M;

http://www.peterRitchie.com/

tshad wrote:
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
You'd probably be better off using a Decimal type


But how do you add cents?

I tried doing:

deductionTotals += 100.15

and got 2 errors:

Literal of type double cannot be implicitly converted to type 'decimal';
use an 'M' suffix to create a literal of this type

and

Operator '+=' cannot be applied to operands of type 'decimal' and
'double'

Do I need to take all my calculations and multiply them by 100?

Thanks,

Tom


Nov 17 '05 #7
<go***********@ peterRitchie.co m> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
The help for CS0664 is pretty clear. By default, doubles cannot be
implicitly converted to decimal. 100.15 is considered a double. To
make it decimal write 100.15M:

deductionTotals += 100.15M;
I got it now.

That answered the question of the error message I was getting. All literals
need to have an M after it when dealing with decimals

If you wanted to add a "double" type variable to a decimal, since it can't
be done implicitly, would you have to use some sort of "Convert" function
first?

Thanks,

Tom
http://www.peterRitchie.com/

tshad wrote:
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
> You'd probably be better off using a Decimal type


But how do you add cents?

I tried doing:

deductionTotals += 100.15

and got 2 errors:

Literal of type double cannot be implicitly converted to type
'decimal';
use an 'M' suffix to create a literal of this type

and

Operator '+=' cannot be applied to operands of type 'decimal' and
'double'

Do I need to take all my calculations and multiply them by 100?

Thanks,

Tom
>

Nov 17 '05 #8
PW
Use:

deductionTotals += 100.15m;

M is a "literal suffix" that tells the compiler to treat your literal as a
Decimal.
"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
You'd probably be better off using a Decimal type


But how do you add cents?

I tried doing:

deductionTotals += 100.15

and got 2 errors:

Literal of type double cannot be implicitly converted to type 'decimal';
use an 'M' suffix to create a literal of this type

and

Operator '+=' cannot be applied to operands of type 'decimal' and
'double'

Do I need to take all my calculations and multiply them by 100?

Thanks,

Tom


Nov 17 '05 #9
"PW" <pw@nospamwante d> wrote in message
news:11******** *****@corp.supe rnews.com...
Use:

deductionTotals += 100.15m;

M is a "literal suffix" that tells the compiler to treat your literal as a
Decimal.
It's interesting that they didn't use "D" instead of "M" :)

Thanks,

Tom

"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
You'd probably be better off using a Decimal type


But how do you add cents?

I tried doing:

deductionTotals += 100.15

and got 2 errors:

Literal of type double cannot be implicitly converted to type
'decimal'; use an 'M' suffix to create a literal of this type

and

Operator '+=' cannot be applied to operands of type 'decimal' and
'double'

Do I need to take all my calculations and multiply them by 100?

Thanks,

Tom



Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3395
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in the wrong direction with the implementation? I'm asking this because I don't have much experience with C++. Thanks in advance. The main problem I see with this class, is that the code which uses it
4
3057
by: MCollins | last post by:
trying to determine a variable type, specifically that a variable is an integer. i tried using type(var) but that only seemed to produce a response in the command line. is there a built in python function to determine if a variable is an integer?
5
3800
by: frankhall36 | last post by:
Yeah, I need some help, I'm not a very good programmer but I've tried a lot of languages, and anyways, I want to start applying programming to physics, and I would like to learn how to make a Variable Type for a little program I am making. Basically a 32byte or up sized long double (which is 16 bytes from what I was told). I just really want to learn how to be able to declare my own type so that I could do things with very good...
2
2101
by: Mike Moore | last post by:
does anyone have an example of how to get the connection string object converted to a string variable type in order for me to call a function?
1
1566
by: tshad | last post by:
I have some code to go through a session collection for my error page routine and I get an error on my objects that I store in session variables. Dim strName as String Dim iLoop as Integer For Each strName in Session.Contents trace.warn(strName & " - " & Session.Contents(strName)) Next
5
11039
by: John | last post by:
Which variable type (c#) can whole the largest whole number? I know this sounds silly but as double and decimal are made for numbers with decimals I am not sure. Also if anybody knows of any library for .NET that would allow me to deal with **VERY** large whole numbers. Thanks in advance JT.
1
3822
by: Sharon | last post by:
I need to write an XML document, that other users can work with to change values and to add elements. My problem is that for each element that me or any other user will add, should have some kind of attribute (or any other solution...) to indicate its type, this type is a variable type to be used in my code (Int32, double, byte, string etc.). I need it so when I'm reading the XML by code (DOM parser), I will be able to know the type of...
4
10305
by: mouseit | last post by:
If I have a variable of some sort, say x, how can I find out what variable type javascript thinks it is? For example, if I've declared x as 5 earlier, how do I know now whether it's a string or a number, isNaN returns false, regardless of whether I declared it as "5" or 5.
2
1439
by: Dan29 | last post by:
Hi guys, I wonder if you can help? I am trying to create Query Definitions in VBA in Access 2002. I'm using the book 'Begining Access 2002 VBA', which shows how to do this. On the example database included on a CD with the book, this is possible without problem. However, when I go to declare a variable of type 'QueryDef' in the Access 2002 databases we use at work, the variable type 'QueryDef' just isn't recognised, nor is it listed in the...
0
8983
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.