473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 14951
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.com

"tshad" <ts**********@ftsolutions.com> wrote in message
news:ek**************@TK2MSFTNGP11.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.ToString( "C" );

cheers,

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

"tshad" <ts**********@ftsolutions.com> wrote in message
news:ek**************@TK2MSFTNGP11.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.googlegr oups.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.googlegr oups.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.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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.googlegr oups.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**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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@nospamwanted> wrote in message
news:11*************@corp.supernews.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**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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

tshad wrote:
<go***********@peterRitchie.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... 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?


You'd use Convert.ToDecimal(dblvalue)

Nov 17 '05 #11

tshad wrote:
"PW" <pw@nospamwanted> wrote in message
news:11*************@corp.supernews.com... It's interesting that they didn't use "D" instead of "M" :)


D or d is used to specify a double literal
F or f is used to specify a float literal
M or m is used to specify a decimal literal

Nov 17 '05 #12

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

Similar topics

5
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...
4
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...
5
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...
2
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
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 ...
5
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...
1
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...
4
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.