473,545 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Currency Math in VB.NET

Just making sure I'm not missing the boat here, but are there any
special routines for doing currency math (fixed precision stuff) in .NET?

The wonderful problems of doing math on decimals tend to shine when
writing accounting software :-)

How are others dealing with this?

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #1
6 9531
On Thu, 11 Aug 2005 08:17:12 -0400, Mitchell Vincent
<mv******@newsg roup.nospam> wrote:
Just making sure I'm not missing the boat here, but are there any
special routines for doing currency math (fixed precision stuff) in .NET?

The wonderful problems of doing math on decimals tend to shine when
writing accounting software :-)

How are others dealing with this?


I think most work in double precision, truncate at 4 decimals, and transfer
the chopped off bits (accumulated) to their own account.
Seriously: I'm missing a currency type as well.

There's a workaround, but it induces a distinct feeling of going back in
time to COBOL.

Remember how you had to write an ADDONETOI subroutine to increment a
variable in COBOL? In .Net there's a currency datatype, but it knows no
operators + - / or *. No mixing of data types - you will NOT divide a
currency type by an integer, unless you yourself implement it as a
function.

Dim x1 As New SqlTypes.SqlMon ey(0.0002)
Dim x2 As New SqlTypes.SqlMon ey(0.0003)
Dim TWO As New SqlTypes.SqlMon ey(2)
Dim x3 As SqlTypes.SqlMon ey = SqlTypes.SqlMon ey.Add(x1, x2)
Dim x4 As SqlTypes.SqlMon ey = SqlTypes.SqlMon ey.Divide(x3, TWO)
Debug.WriteLine (x4.ToString)

The result is 0.0003: standard rounding applied to .00025, whereas the
Math.Round function on a Double would, to many one's surprise, use banker's
rounding and say it's 0.0002. But that's topic for another discussion ;)
Nov 21 '05 #2
Lucvdv wrote:
Remember how you had to write an ADDONETOI subroutine to increment a
variable in COBOL? In .Net there's a currency datatype, but it knows no
operators + - / or *. No mixing of data types - you will NOT divide a
currency type by an integer, unless you yourself implement it as a
function.

Dim x1 As New SqlTypes.SqlMon ey(0.0002)
Dim x2 As New SqlTypes.SqlMon ey(0.0003)
Dim TWO As New SqlTypes.SqlMon ey(2)
Dim x3 As SqlTypes.SqlMon ey = SqlTypes.SqlMon ey.Add(x1, x2)
Dim x4 As SqlTypes.SqlMon ey = SqlTypes.SqlMon ey.Divide(x3, TWO)
Debug.WriteLine (x4.ToString)

The result is 0.0003: standard rounding applied to .00025, whereas the
Math.Round function on a Double would, to many one's surprise, use banker's
rounding and say it's 0.0002. But that's topic for another discussion ;)

So you're using the SqlTypes.SqlMon ey type for all your math operations?

It seems like the issue you just showed would be a reason not to!

I'm only in need of 2 precision, as I'm not dealing with anything
smaller than a cent.

Thanks!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #3
On Thu, 11 Aug 2005 09:53:12 -0400, Mitchell Vincent
<mv******@newsg roup.nospam> wrote:
So you're using the SqlTypes.SqlMon ey type for all your math operations?
No, I got off lucky: I can use longs instead (no invoicing type of
applications, and all amounts I work with are multiples of 1 euro).
It seems like the issue you just showed would be a reason not to!
That depends. In my opinion it's Math.Round that's giving the wrong
result, not the other one.
I'm only in need of 2 precision, as I'm not dealing with anything
smaller than a cent.


The currency type that exists in VB6 for example is also fixed 4 digits.

Over here we even have a law that stipulates that all computations on
currency values must be performed with an internal precision of 4 decimals
(0.0001 euros), and only the final printed results should be rounded to two
decimals.

As long as only addition and subtraction are used it's the same, but
percentages is where it begins to matter, and in Europe there's no way to
get around those (tax: the VAT nuisance).

It's perfectly possible here to have an invoice where the printed numbers
don't add up, if VAT amounts are listed per individual item (which is not a
requirement).
Nov 21 '05 #4
Mitchell,
I normally do all my "currency math" with System.Decimal, of course when I
use multiplication or division I need to be certain to round the numbers
"correctly" .

In VB 2005 you can use Operator Overloading to define your own "special
routines" that handle any specific rounding issues, thus encapsulating any
possible rounding concerns. VB 2005's Operator Overloading also allows you
to leverage the overloaded operators on SqlMoney.

Hope this helps
Jay

"Mitchell Vincent" <mv******@newsg roup.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
| Just making sure I'm not missing the boat here, but are there any
| special routines for doing currency math (fixed precision stuff) in .NET?
|
| The wonderful problems of doing math on decimals tend to shine when
| writing accounting software :-)
|
| How are others dealing with this?
|
| --
| - Mitchell Vincent
| - kBilling - Invoices Made Easy!
| - http://www.k-billing.com
Nov 21 '05 #5
Jay B. Harlow [MVP - Outlook] wrote:
Mitchell,
I normally do all my "currency math" with System.Decimal, of course when I
use multiplication or division I need to be certain to round the numbers
"correctly" .


How do you handle it exactly?

I've always stored my money values as ints instead of fooling with the
decimal game but now that is more of a problem than a help.

I like the overloading idea - do you have any examples that I might go by?

Thanks a million!

--
- Mitchell Vincent
- kBilling - Invoices Made Easy!
- http://www.k-billing.com
Nov 21 '05 #6
Mitchell,
I use Math.Round & Decimal.Round most of the time.

..NET 2.0 (VS.NET 2005, due out late in 2005) has overloaded Math.Round to
account for other rounding methods (mid point up or even/bankers).

http://msdn2.microsoft.com/library/m...us,vs.80).aspx
As suggested in the following previous post of mine, I find banker's
rounding (the current Math.Round function) to be the "logical" choice.

---x--- cut here ---x---
The way I understand it is:

Rather then always rounding the mid point up, "half of the time" you are
rounding it up & "half of the time" you are rounding it down.

The two "half of the time" together will average themselves out of the
picture.

When you always rounding the mid point up, then you start accumulating all
the fractions, rather then average them out...

The following demonstrates this:

Public Shared Sub Main()
Dim value1 As Decimal = 1.235D
Dim value2 As Decimal = 1.245D

Dim value3 As Decimal = value1 + value2

Dim value4 As Decimal = Decimal.Round(v alue1, 2) +
Decimal.Round(v alue2, 2)
Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2)

Debug.WriteLine (value1, "value1")
Debug.WriteLine (value2, "value2")
Debug.WriteLine (value3, "value3")
Debug.WriteLine (value4, "value4")
Debug.WriteLine (value5, "value5")
End Sub

' may not handle negative value correctly...
Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals
As Integer) As Decimal
decimals = CInt(10 ^ decimals)
value *= decimals
value = Decimal.Truncat e(value + 0.5D)
value /= decimals
Return value
End Function

Notice how value4 & value5 are off by 1, if you are lot of rounding, this
can add up significantly. When you use banking rounding, this difference
will not add up as quickly or as much...
---x--- cut here ---x---

| I like the overloading idea - do you have any examples that I might go by?
NOTE: You need C# 2002 or higher or VB 2005 or higher:

http://msdn.microsoft.com/library/de...verloading.asp

http://msdn.microsoft.com/library/de...ml/vs03h12.asp

http://www.panopticoncentral.net/arc...7/01/1338.aspx

Hope this helps
Jay

"Mitchell Vincent" <mv******@newsg roup.nospam> wrote in message
news:eC******** ******@TK2MSFTN GP09.phx.gbl...
| Jay B. Harlow [MVP - Outlook] wrote:
| > Mitchell,
| > I normally do all my "currency math" with System.Decimal, of course when
I
| > use multiplication or division I need to be certain to round the numbers
| > "correctly" .
|
| How do you handle it exactly?
|
| I've always stored my money values as ints instead of fooling with the
| decimal game but now that is more of a problem than a help.
|
| I like the overloading idea - do you have any examples that I might go by?
|
| Thanks a million!
|
| --
| - Mitchell Vincent
| - kBilling - Invoices Made Easy!
| - http://www.k-billing.com
Nov 21 '05 #7

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

Similar topics

2
4426
by: techy techno | last post by:
hii Experts..!! I need someone to tell me where I can get a Currency Converter like http://cconv.textor.com please can someone tell me where I can get it I need it for free + I dont need any link of the co. the code for this is here but when I change it it just says unauthorize
3
2117
by: Abby Lee | last post by:
I found the following script somewhere so I can change values to money before the form is printed. The page works for most people but not some. It worked fine for me but I get the yellow tri-angle with an exclamation point in the bottom left part of the window. When I told my IE to display a notification about every error...it told me there...
6
1688
by: News Guy | last post by:
Hello, Can someone tell me an easy way to convert a number with many trailing digits to a currency format and adding the '$' sign? Thanks. example Convert 34.77389993 to $34.77 Thanks, News Guy
2
574
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar multiply(by int)& show, have a constructor w/ & w/out arguments. Header file should have private data & all 6 functions from above.Class definition file...
7
4054
by: meenasamy | last post by:
Hi all, i need to create a function that takes three parameters( Original currency, needed currency, amount) i need to convert from the original currency to the needed currency the amount and return the new amount in the needed currency, I need this function to work real time (obtaining real time currency exchange rates), any ideas???
7
3760
by: tararreb | last post by:
#include<stdio.h> /*This line is standard input output, # is directive, include is keyword, and stdio.h is header file*/ #include<stdlib.h> /*This line is standard input output, # is directive, include is keyword, and stdlib.h is standard library defininition*/ #include<math.h> /*This line is standard input output, # is directive, include...
9
2560
by: ballygowanboy | last post by:
ok, so i've my front end shop code almost working, the quantity and price all add up........ now i need to add a curency conversion function, euro (default), dollar, and uk pound. right, i'm not sure if i should have 3 seperate functions, that do the math on the total box, and add the required, €, £ or $ icon....... it would be best to put this...
16
12040
by: Alexio | last post by:
Am new to javascript. I need to format the currency in the as numbers are entered in textboxes. I can format the currency in the calculations and display in the totals but can not figure out how to format in input textboxes. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>...
1
4269
by: Brian | last post by:
Hello, I am new to Microsoft access 2007 and I have created a new database. There is a dollar about that I have to change from text to currency format. Also, there is a zip code that I have to change from text to number format. When I try to change them from text to either currency or number format I get an error stating "Some data may be...
0
7487
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...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
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. ...
0
7934
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...
0
6003
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...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
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...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.