<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Avyas2</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Avyas2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Avyas2"/>
	<updated>2026-07-01T17:29:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68638</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68638"/>
		<updated>2012-10-27T00:38:33Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 &amp;lt;ref name = WikiRef/&amp;gt; is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support&amp;lt;ref name = PressmanRef/&amp;gt;. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built&amp;lt;ref name = AnalysisRef /&amp;gt;. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc&amp;lt;ref name = PressmanRef/&amp;gt;. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail&amp;lt;ref name = PressmanRef/&amp;gt;. All the elements of the solution are described along with the relationship among them&amp;lt;ref name = WikiArRef/&amp;gt;. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself &amp;lt;ref name = ImRef/&amp;gt;, as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together&amp;lt;ref name = PressmanRef/&amp;gt;. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability&amp;lt;ref name = SysRef/&amp;gt;. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system.&amp;lt;ref name = PressmanRef/&amp;gt; After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use&amp;lt;ref name = PressmanRef/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding&amp;lt;ref name = AdvRef/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations&amp;lt;ref name = AdvRef/&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
* Initially the customers are not very clear about the requirements for complex projects. The waterfall model does not have scope for the inclusion of new requirements once requirement phase is over.&lt;br /&gt;
&lt;br /&gt;
* It is not a very good model for long and ongoing projects.&lt;br /&gt;
&lt;br /&gt;
* In this model, we do not get the look and feel of the actual system, until very late in the life cycle.&lt;br /&gt;
&lt;br /&gt;
* High amount of risk and uncertainty is attached with this model.&lt;br /&gt;
&lt;br /&gt;
* Potential delay in identifying the risks which might lead to disastrous results later in the cycle.&lt;br /&gt;
&lt;br /&gt;
* Many a times, a feasible looking design on paper may turn out to be expensive or difficult in the implementation phase requiring a redesign and hence destroying the distinction between the different phases.&amp;lt;ref name = AdvRef/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Time to market is very high as compared to other models for software development.&lt;br /&gt;
&lt;br /&gt;
* The waterfall model does not allow us to go back to a phase after its completion.&amp;lt;ref name = AnalysisRef/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* For contemporary projects, it is very difficult to follow a sequential flow in the software development process because of the dynamic nature of the requirements.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = AnalysisRef&amp;gt;  http://www.slideshare.net/BHARGAV_VISANI/waterfall-model &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = PressmanRef&amp;gt; Software Engineering, A Practitioner's Approach by Roger S. Pressman&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = WikiRef&amp;gt; http://en.wikipedia.org/wiki/Waterfall_model&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = WikiArRef&amp;gt; http://en.wikipedia.org/wiki/Software_architecture&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ImRef&amp;gt; http://www.ianswer4u.com/2011/11/waterfall-model.html#axzz2ARy246Eu&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = SysRef&amp;gt; http://en.wikipedia.org/wiki/System_testing&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = AdvRef&amp;gt; http://www.techrepublic.com/article/understanding-the-pros-and-cons-of-the-waterfall-model-of-software-development/6118423&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68626</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68626"/>
		<updated>2012-10-27T00:34:25Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 &amp;lt;ref name = WikiRef/&amp;gt; is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support&amp;lt;ref name = PressmanRef/&amp;gt;. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built&amp;lt;ref name = AnalysisRef /&amp;gt;. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc&amp;lt;ref name = PressmanRef/&amp;gt;. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail&amp;lt;ref name = PressmanRef/&amp;gt;. All the elements of the solution are described along with the relationship among them&amp;lt;ref name = WikiArRef/&amp;gt;. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself &amp;lt;ref name = ImRef/&amp;gt;, as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together&amp;lt;ref name = PressmanRef/&amp;gt;. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability&amp;lt;ref name = SysRef/&amp;gt;. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
* Initially the customers are not very clear about the requirements for complex projects. The waterfall model does not have scope for the inclusion of new requirements once requirement phase is over.&lt;br /&gt;
&lt;br /&gt;
* It is not a very good model for long and ongoing projects.&lt;br /&gt;
&lt;br /&gt;
* In this model, we do not get the look and feel of the actual system, until very late in the lifecycle.&lt;br /&gt;
&lt;br /&gt;
* High amount of risk and uncertainty is attached with this model.&lt;br /&gt;
&lt;br /&gt;
* Potential delay in identifying the risks which might lead to disastrous results later in the cycle.&lt;br /&gt;
&lt;br /&gt;
* Many a times, a feasible looking design on paper may turn out to be expensive or difficult in the implementation phase requiring a redesign and hence destroying the distinction between the different phases.&lt;br /&gt;
&lt;br /&gt;
* Time to market is very high as compared to other models for software development.&lt;br /&gt;
&lt;br /&gt;
* The waterfall model does not allow us to go back to a phase after its completion.&lt;br /&gt;
&lt;br /&gt;
* For contemporary projects, it is very difficult to follow a sequential flow in the software development process because of the dynamic nature of the requirements.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = AnalysisRef&amp;gt;  http://www.slideshare.net/BHARGAV_VISANI/waterfall-model &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = PressmanRef&amp;gt; Software Engineering, A Practitioner's Approach by Roger S. Pressman&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = WikiRef&amp;gt; http://en.wikipedia.org/wiki/Waterfall_model&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = WikiArRef&amp;gt; http://en.wikipedia.org/wiki/Software_architecture&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ImRef&amp;gt; http://www.ianswer4u.com/2011/11/waterfall-model.html#axzz2ARy246Eu&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = SysRef&amp;gt; http://en.wikipedia.org/wiki/System_testing&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68616</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68616"/>
		<updated>2012-10-27T00:27:50Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 &amp;lt;ref name = WikiRef/&amp;gt; is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support&amp;lt;ref name = PressmanRef/&amp;gt;. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built&amp;lt;ref name = AnalysisRef /&amp;gt;. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc&amp;lt;ref name = PressmanRef/&amp;gt;. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
* Initially the customers are not very clear about the requirements for complex projects. The waterfall model does not have scope for the inclusion of new requirements once requirement phase is over.&lt;br /&gt;
&lt;br /&gt;
* It is not a very good model for long and ongoing projects.&lt;br /&gt;
&lt;br /&gt;
* In this model, we do not get the look and feel of the actual system, until very late in the lifecycle.&lt;br /&gt;
&lt;br /&gt;
* High amount of risk and uncertainty is attached with this model.&lt;br /&gt;
&lt;br /&gt;
* Potential delay in identifying the risks which might lead to disastrous results later in the cycle.&lt;br /&gt;
&lt;br /&gt;
* Many a times, a feasible looking design on paper may turn out to be expensive or difficult in the implementation phase requiring a redesign and hence destroying the distinction between the different phases.&lt;br /&gt;
&lt;br /&gt;
* Time to market is very high as compared to other models for software development.&lt;br /&gt;
&lt;br /&gt;
* The waterfall model does not allow us to go back to a phase after its completion.&lt;br /&gt;
&lt;br /&gt;
* For contemporary projects, it is very difficult to follow a sequential flow in the software development process because of the dynamic nature of the requirements.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = AnalysisRef&amp;gt;  http://www.slideshare.net/BHARGAV_VISANI/waterfall-model &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = PressmanRef&amp;gt; Software Engineering, A Practitioner's Approach by Roger S. Pressman&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = WikiRef&amp;gt; http://en.wikipedia.org/wiki/Waterfall_model&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68610</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68610"/>
		<updated>2012-10-27T00:25:58Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built&amp;lt;ref name = AnalysisRef /&amp;gt;. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc&amp;lt;ref name = PressmanRef/&amp;gt;. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
* Initially the customers are not very clear about the requirements for complex projects. The waterfall model does not have scope for the inclusion of new requirements once requirement phase is over.&lt;br /&gt;
&lt;br /&gt;
* It is not a very good model for long and ongoing projects.&lt;br /&gt;
&lt;br /&gt;
* In this model, we do not get the look and feel of the actual system, until very late in the lifecycle.&lt;br /&gt;
&lt;br /&gt;
* High amount of risk and uncertainty is attached with this model.&lt;br /&gt;
&lt;br /&gt;
* Potential delay in identifying the risks which might lead to disastrous results later in the cycle.&lt;br /&gt;
&lt;br /&gt;
* Many a times, a feasible looking design on paper may turn out to be expensive or difficult in the implementation phase requiring a redesign and hence destroying the distinction between the different phases.&lt;br /&gt;
&lt;br /&gt;
* Time to market is very high as compared to other models for software development.&lt;br /&gt;
&lt;br /&gt;
* The waterfall model does not allow us to go back to a phase after its completion.&lt;br /&gt;
&lt;br /&gt;
* For contemporary projects, it is very difficult to follow a sequential flow in the software development process because of the dynamic nature of the requirements.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = AnalysisRef&amp;gt;  http://www.slideshare.net/BHARGAV_VISANI/waterfall-model &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = PressmanRef&amp;gt; Software Engineering, A Practitioner's Approach by Roger S. Pressman&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68585</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68585"/>
		<updated>2012-10-27T00:20:06Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;br /&gt;
&lt;br /&gt;
* Initially the customers are not very clear about the requirements for complex projects. The waterfall model does not have scope for the inclusion of new requirements once requirement phase is over.&lt;br /&gt;
&lt;br /&gt;
* It is not a very good model for long and ongoing projects.&lt;br /&gt;
&lt;br /&gt;
* In this model, we do not get the look and feel of the actual system, until very late in the lifecycle.&lt;br /&gt;
&lt;br /&gt;
* High amount of risk and uncertainty is attached with this model.&lt;br /&gt;
&lt;br /&gt;
* Potential delay in identifying the risks which might lead to disastrous results later in the cycle.&lt;br /&gt;
&lt;br /&gt;
* Many a times, a feasible looking design on paper may turn out to be expensive or difficult in the implementation phase requiring a redesign and hence destroying the distinction between the different phases.&lt;br /&gt;
&lt;br /&gt;
* Time to market is very high as compared to other models for software development.&lt;br /&gt;
&lt;br /&gt;
* The waterfall model does not allow us to go back to a phase after its completion.&lt;br /&gt;
&lt;br /&gt;
* For contemporary projects, it is very difficult to follow a sequential flow in the software development process because of the dynamic nature of the requirements.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68582</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68582"/>
		<updated>2012-10-27T00:17:13Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
* As it is the most basic model for Software Development, it is very simple and easy to use.&lt;br /&gt;
&lt;br /&gt;
* It follows a very disciplined approach.&lt;br /&gt;
&lt;br /&gt;
* It is easily manageable because, every phase is very well defined and each phase has a set of deliverables and a review process.&lt;br /&gt;
&lt;br /&gt;
* All the phases are completed one at a time and hence there is no confusion.&lt;br /&gt;
&lt;br /&gt;
* It is the best for smaller projects as the requirements are very well understood.&lt;br /&gt;
&lt;br /&gt;
* All the phases have a very clearly defined start and end points, thus the progress of the project can be conclusively identified.&lt;br /&gt;
&lt;br /&gt;
* As importance is given to the Requirement gathering and the Design phase even before writing a single line of code, there is a very low probability of wasting time and effort while coding.&lt;br /&gt;
&lt;br /&gt;
* As the first two phases end in the production of a formal specification, the waterfall model can aid efficient knowledge transfer when the team members are dispersed in different locations.&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68493</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68493"/>
		<updated>2012-10-26T23:55:19Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;2w2. Waterfall model&lt;br /&gt;
Describe what the waterfall model is. Describe the activities or steps that the waterfall model consists of. State and argue about the core principle behind using the waterfall model e.g. why it is good and why it is bad. What are the advantages and disadvantages of waterfall model? &lt;br /&gt;
&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Advantages ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Disadvantages ==&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68488</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68488"/>
		<updated>2012-10-26T23:53:42Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
'''Analysis and Requirement Gathering'''&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
''' Design '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Implementation''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' Testing ''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Deployment and Support'''&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68482</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68482"/>
		<updated>2012-10-26T23:51:35Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Phases ==&lt;br /&gt;
&lt;br /&gt;
== Analysis and Requirement Gathering ==&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment and Support ==&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68480</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68480"/>
		<updated>2012-10-26T23:50:57Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFModel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Analysis and Requirement Gathering ==&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment and Support ==&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68478</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68478"/>
		<updated>2012-10-26T23:50:30Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:WFmodel.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Analysis and Requirement Gathering ==&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment and Support ==&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:WFModel.png&amp;diff=68476</id>
		<title>File:WFModel.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:WFModel.png&amp;diff=68476"/>
		<updated>2012-10-26T23:50:05Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: The diagram shows the linear sequential model of software development.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The diagram shows the linear sequential model of software development.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68465</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68465"/>
		<updated>2012-10-26T23:47:07Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:Waterfallab.png]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Analysis and Requirement Gathering ==&lt;br /&gt;
&lt;br /&gt;
This is the first and foremost phase in the Waterfall model or Linear Sequential Model for Software Development. This phase includes meeting with the customer and gathering all requirements specific to the project. This can be considered as the most important phase of the model, because if there is any misinterpretation during this phase then it can lead to validation issues later. The gathered requirements have to be unambiguous. Gathering the correct requirements is very important so that the customer is satisfied with the final product that is built[1]. This stage basically deals with gathering both the functional and the non functional requirements of the project. Functional requirements are those requirements which are the actual features for which the project is being made. Non-functional requirements are those implicit requirements that all the projects are expected to have such as Scalability, Maintainability etc. The Waterfall model works very closely with deliverables. Hence, the deliverables that this stage produces is Software requirement specification(SRS), Acceptance Test Plan and the System Test Plan. After the requirements are collected accurately, we move on to the next phase which is the Design phase.&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Designing a software is a process of deriving at the high level solution of the problem that the software aims to solve.The design considers the requirements gathered from in the Requirement analysis phase and build a solution based on the given constraints. The design encompasses the definition of the basic properties of the software solution such as data structures, software architecture, interface representations and algorithmic detail. All the elements of the solution are described along with the relationship among them. Software re-use decisions are also taken at this stage. If some components are eligible for reuse they must be described and incorporated in the design. The deliverable of this phase consists of the High Level Design Document (HLD), the Detailed Design Documents (DLD) along with unit test cases and integration tests. The HLD gives an overview of the system and its various components. A separate DLD is made for all such individual components detailing the design considerations for each. Test cases are also written in the design phase so that the verification and validation of the design can be done on the implementation of the solution. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Implementation ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the third phase in the waterfall model. After we have a thorough design phase, we can go ahead with the implementation phase. In this phase the design is converted into machine readable form. Actual coding is done in this phase and Programs are created. If Design is done sufficiently then implementation can be done with a greater degree of accuracy. In this phase we divide the Software Module into individual units. A unit is a logically separable part of the software. In this phase, along with the code generation part, Unit testing of individual units is also carried out. Unit testing is performed by the developer itself [2], as he is the best person who knows the code and can judge it whether it is meeting the specification or not. The deliverable that we get at the end of this phase is a Unit Tested Code.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Testing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the unit testing is done by the programmers while coding their individual components. In the testing phase, the integration and system testing is carried out. The integration tests focus on the interoperability of the independently developed components of the software. The primary focus is on making sure that they are able to function together. Once that is done, the system testing is carried out. The test cases and plans are based on the System Requirement Specification (SRS).Here the software is tested against the originally expected requirements and behaviors. Whenever an expectation is not met, a flaw is recorded. &lt;br /&gt;
System testing includes the validation of the software based on parameters such as usability, security, accessibility and reliability. &lt;br /&gt;
The functional and non-functional requirements must be tested and the focus is on uncovering maximum numbers of errors and bugs before the system is handed over the customer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Deployment and Support ==&lt;br /&gt;
&lt;br /&gt;
This is the final phase of the Waterfall model. When we reach this phase, we already have a completely functioning and fully tested system on our hand. What needs to be done in this phase is that we need to install the system on the user’s environment. After it is successfully installed, we need to train the customer on how to use the system. After the customer is satisfied with the system, we need to take the approval that the project is officially complete and it is meeting all the requirements that were made in the first phase of the model. In terms of deliverable for this phase, we have to generate a user manual for the user which he can refer if he has any problem understanding the system.  This phase does not end here. If in future the customer has some problems with the system, then it is the responsibility of the developers to get the system up and running if it is down due to some errors. The errors can be introduced due to many reasons. Some of which can be exceptions in the embedded software, or changes in the external environment in which it was deployed or even if the customer wants some additional features in the system.&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68450</id>
		<title>File:Waterfallab.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68450"/>
		<updated>2012-10-26T23:40:00Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: uploaded a new version of &amp;amp;quot;File:Waterfallab.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68448</id>
		<title>File:Waterfallab.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68448"/>
		<updated>2012-10-26T23:39:08Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: uploaded a new version of &amp;amp;quot;File:Waterfallab.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68367</id>
		<title>File:Waterfallab.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Waterfallab.png&amp;diff=68367"/>
		<updated>2012-10-26T22:49:03Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68363</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68363"/>
		<updated>2012-10-26T22:48:23Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:Waterfallab.png]]&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68351</id>
		<title>CSC/ECE 517 Fall 2012/ch2a 2w2 ab</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch2a_2w2_ab&amp;diff=68351"/>
		<updated>2012-10-26T22:43:01Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: Created page with &amp;quot; == The Waterfall Model == The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Deve...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== The Waterfall Model ==&lt;br /&gt;
The linear sequential process of software development described by  Winston W. Royce in 1970 is popularly known as the Waterfall Model of Software Development. Also referred to as the classic life cycle, the linear sequential model presents a systematic approach that is based on progression from one stage to another namely requirement analysis, design, implementation, testing and finally support. Even after the adaptation of many new methodologies such as prototyping and agile, the classic approach presents a very strong case for scenarios where the requirements are fixed and generally helps in understanding the evolution of software engineering processes. The approach is very similar to one followed in many other engineering disciplines such as manufacturing and construction. &lt;br /&gt;
&lt;br /&gt;
[[File:Waterfall.png]]&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67334</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67334"/>
		<updated>2012-10-09T00:59:48Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded.&lt;br /&gt;
*Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information.&lt;br /&gt;
*The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
*Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view.&lt;br /&gt;
*The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type*&lt;br /&gt;
*Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*Doesn't support multiple currency types in a single object.&lt;br /&gt;
*A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided.&lt;br /&gt;
*While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not.&lt;br /&gt;
*Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often.&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
*As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*This implementation does not support an arbitrary number of decimal points.&lt;br /&gt;
*Since the currency conversion data is set within the Currency object itself there is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates.&lt;br /&gt;
*The current implementation can only convert to and from the default currency type.&lt;br /&gt;
*Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems&amp;lt;ref name = javaDBRef/&amp;gt; . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
*BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
*The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
*An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
*The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
*Primitive types double and float carry small rounding differences.&lt;br /&gt;
*Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
*Not much overhead with maintaining the money class objects.&lt;br /&gt;
*Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation&amp;lt;ref name = cRef/&amp;gt; from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
*This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
*The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaDBRef&amp;gt; http://www.firstsql.com/javaobjects.shtml&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = cRef&amp;gt; http://www.di-mare.com/adolfo/p/money.htm &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67333</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67333"/>
		<updated>2012-10-09T00:58:10Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded.&lt;br /&gt;
*Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information.&lt;br /&gt;
*The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view.&lt;br /&gt;
*The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type*&lt;br /&gt;
*Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*Doesn't support multiple currency types in a single object.&lt;br /&gt;
*A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided.&lt;br /&gt;
*While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not.&lt;br /&gt;
*Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often.&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
*Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
*As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
*This implementation does not support an arbitrary number of decimal points.&lt;br /&gt;
*Since the currency conversion data is set within the Currency object itself there is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates.&lt;br /&gt;
*The current implementation can only convert to and from the default currency type.&lt;br /&gt;
*Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems&amp;lt;ref name = javaDBRef/&amp;gt; . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation&amp;lt;ref name = cRef/&amp;gt; from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaDBRef&amp;gt; http://www.firstsql.com/javaobjects.shtml&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = cRef&amp;gt; http://www.di-mare.com/adolfo/p/money.htm &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67332</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67332"/>
		<updated>2012-10-09T00:37:58Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems&amp;lt;ref name = javaDBRef/&amp;gt; . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation&amp;lt;ref name = cRef/&amp;gt; from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaDBRef&amp;gt; http://www.firstsql.com/javaobjects.shtml&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = cRef&amp;gt; http://www.di-mare.com/adolfo/p/money.htm &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67331</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67331"/>
		<updated>2012-10-09T00:36:39Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems&amp;lt;ref name = javaDBRef/&amp;gt; . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaDBRef&amp;gt; http://www.firstsql.com/javaobjects.shtml&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67330</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67330"/>
		<updated>2012-10-09T00:35:33Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems&amp;lt;ref name = javaDBRef/&amp;gt; . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaDBRef&amp;gt; http://www.firstsql.com/javaobjects.shtml&amp;lt;ref/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67329</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67329"/>
		<updated>2012-10-09T00:33:10Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet&amp;lt;ref name = javaPracRef/&amp;gt; that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaPracRef&amp;gt; http://www.javapractices.com/topic/TopicAction.do?Id=13&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67327</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67327"/>
		<updated>2012-10-09T00:31:44Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Some of the most common operations for money types are implemented in the Currency class &amp;lt;ref name = javaRef/&amp;gt;, which encapsulates, among other things, standard identifiers for currencies around the world. However, the lack of a standard Money Class in the Java library has resulted in users employing different schemes to conduct operations on money.Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = javaRef&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67326</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67326"/>
		<updated>2012-10-09T00:28:05Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation &amp;lt;ref name = intRef /&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design&amp;lt;ref name = skrienRef/&amp;gt;.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67324</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67324"/>
		<updated>2012-10-09T00:26:28Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation === &amp;lt;ref name = intRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal&amp;lt;ref name = pythonDecRef/&amp;gt;, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonDecRef&amp;gt; http://docs.python.org/library/decimal.html&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67323</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67323"/>
		<updated>2012-10-09T00:24:48Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation === &amp;lt;ref name = intRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/ &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67322</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67322"/>
		<updated>2012-10-09T00:23:44Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.''&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
=== Floating point Representation ===&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
=== Integer Representation === &amp;lt;ref name = intRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
=== Two Integers ===&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Class Representation ===&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Practically Used Approaches ==&lt;br /&gt;
&lt;br /&gt;
=== Ruby - Money Gem ===&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Python - python-money ===&lt;br /&gt;
&lt;br /&gt;
The python-money implementation&amp;lt;ref name = pythonRef /&amp;gt; of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== C++ ===&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
=== money ===&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== smallmoney ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
==== Advantages ====&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
==== Disadvantages ====&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&lt;br /&gt;
&amp;lt;ref name = pythonRef&amp;gt; http://code.google.com/p/python-money/&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67320</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67320"/>
		<updated>2012-10-09T00:20:36Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result. &amp;lt;ref name = floatRef /&amp;gt; &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&amp;lt;ref name = intRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&amp;lt;ref name = currencyClass /&amp;gt;&amp;lt;ref name = skrienRef /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217&amp;lt;ref name = ISORef /&amp;gt; code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = floatRef&amp;gt; http://www.drdobbs.com/jvm/java-monetary-data/184405653 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = intRef&amp;gt;http://users.csc.calpoly.edu/~jdalbey/SWE/SampleCode/Money.java &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = skrienRef&amp;gt; Skrien, Dale. Object-Oriented Design Using Java. McGraw Hill, 2009, p. 184 &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = currencyClass&amp;gt; http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Currency.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = ISORef&amp;gt; http://www.abstracttechnology.com/standard/iso4217.html&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67174</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67174"/>
		<updated>2012-10-04T02:14:05Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above. The approach has been modified to use Expert Design Pattern. &lt;br /&gt;
&lt;br /&gt;
'''Design Considerations'''&lt;br /&gt;
&lt;br /&gt;
*Encapsulation of all major calculation functions. The programmer does not have to know details of the implementations.&lt;br /&gt;
*Using  custom data type  logical errors can be discovered. Many of these go without notice if primitive data-types are used because the compiler assumes the correct usage of the data without regard to the contextual use. Thus operations can be performed only by specialized functions and this prevents erroneous calculations.&lt;br /&gt;
*Most different types of currency have a similar implementation requirement. Thus the author suggests defining a currency instance variable to store currency specific values such as its symbol. The operations remain common across different currencies. &lt;br /&gt;
*A real world implementation of money must be able to support multiple types of currencies. The author suggest the implementation of a mixed money class that has supports all operations similar to money but can handle multiple currency types like a wallet. It stores the type and value of multiple types of currencies. The objects must be able to exchange values and support similar functions thus an interface is used which both money and mixed money must implement.&lt;br /&gt;
*The issue of conversion of values between currencies and comparison of values of different types, the author suggests a currency converter class. It must be recognized that the exchange rates are dynamic and thus cannot be coded in the mixed money class. The currency converter class only deals with the setting and usage of exchange rates. Objects of type money can use it to get multiplication factors and perform their own calculations.    &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This approach can be summarized by the following UML diagram.&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67164</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67164"/>
		<updated>2012-10-04T02:08:54Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Skrien’s Approach ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above.&lt;br /&gt;
This approach can be summarized by the following UML diagram.&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67157</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67157"/>
		<updated>2012-10-04T02:07:22Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
'''Skrien’s Approach'''&lt;br /&gt;
&lt;br /&gt;
The representation discussed by Skrien in the book Object Oriented Programming Using Java, is built over the single class representation above.&lt;br /&gt;
This approach can be summarized by the following UML diagram.&lt;br /&gt;
&lt;br /&gt;
[[File:Skrein.png]]&lt;br /&gt;
&lt;br /&gt;
The classes SimpleMoney and MixedMoney are used to represent money in cases when money belongs to only a single currency and when it belongs in parts to multiple currencies respectively. Simple money is a class as discussed in the previous section. Mixed money has the added features giving the list of currencies its object contains and the amounts in each currency and briefly looks as follows – &lt;br /&gt;
public class MixedMoney implements Money&lt;br /&gt;
{&lt;br /&gt;
public MixedMoney() {..}&lt;br /&gt;
public string toString() {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public int compareTo(Money o) {..}&lt;br /&gt;
public Money plus(Money) {..}&lt;br /&gt;
public Money minus(Money) {..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
public Collection getCurrencies() {..}&lt;br /&gt;
public long getAmount (Currency currency) {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
For the ease of combining the objects of the two classes SimpleMoney and MixedMoney, the interface Money is used. This interface contains the common methods that need implementation in both the classes.&lt;br /&gt;
&lt;br /&gt;
public interface Money extends Comparable&amp;lt;Money&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
public int compareTo(Money o);&lt;br /&gt;
public Money plus(Money money);&lt;br /&gt;
public Money minus(Money money);&lt;br /&gt;
public Money times(double factor);&lt;br /&gt;
public Money dividedBy(double divisor);&lt;br /&gt;
public Money negate(); &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67078</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67078"/>
		<updated>2012-10-04T01:31:36Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. An example of sample class representation is presented below.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Money{&lt;br /&gt;
private long amount;&lt;br /&gt;
private String currency;&lt;br /&gt;
public Money (long amount, String currency) {&lt;br /&gt;
this.amount = amount;&lt;br /&gt;
this.currency  = currency;&lt;br /&gt;
}&lt;br /&gt;
public String toString () {&lt;br /&gt;
return amount/100 + “.” + amount%100 + currency;&lt;br /&gt;
}&lt;br /&gt;
public long getAmount() {..}&lt;br /&gt;
public String getCurrency() {…}&lt;br /&gt;
public int compareTo(Money m) {..}&lt;br /&gt;
public Boolean equals(Object o) {..}&lt;br /&gt;
public int hashCode() {..}&lt;br /&gt;
public Money plus (Money) {..}&lt;br /&gt;
public Money minus(Money){..}&lt;br /&gt;
public Money times(double factor) {..}&lt;br /&gt;
public Money dividedBy(double divisor) {..}&lt;br /&gt;
public Money negate() {..}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Such a class takes the currency attribute along with the amount to be represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money. The implementation of the toString method however becomes a terrible design with a large if-then-else statement for distinguishing between the layouts of 170 currencies.  &lt;br /&gt;
Such a class is fine as long as the money is in a single currency. It can however not be used if the money belongs to different currencies. For example we have a “wallet” with 10 Dollars, 50 Yens and 100 Rupees.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67042</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67042"/>
		<updated>2012-10-04T01:21:03Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The datatype is very rigid in terms of precision. The program has to use 4 decimal places. It is prone to precision losses when used in calculations. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67028</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=67028"/>
		<updated>2012-10-04T01:17:53Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation of money in Database ==&lt;br /&gt;
&lt;br /&gt;
Most custom implementations in the programming languages for money require a custom storage definition based on a combination of primitive data-types. Microsoft SQL Server provides an specific data-type to store currency values.&amp;lt;ref name = mssqlserverreference /&amp;gt;. Although the representation is easy to store and retrieve but is not very widely used. It is important to analyse the relative advantages and disadvantages of this approach. &lt;br /&gt;
&lt;br /&gt;
'''money'''&lt;br /&gt;
&lt;br /&gt;
This data-type can be used to store monetary data values from -2^63 (-922,337,203,685,477.5808) through 2^63 - 1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''smallmoney'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This can be used for monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The precision of the money data-type is like a DECIMAL(9,4) but technically is different in terms of actual implementation. It is similar to an integer implementation with an implied decimal point. This is enough for most implementations. The money data-type also supports formatting with a number of separators. &lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref name = mssqlserverreference&amp;gt;  http://msdn.microsoft.com/en-us/library/aa258271(v=sql.80).aspx &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66936</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66936"/>
		<updated>2012-10-04T00:54:47Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66931</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66931"/>
		<updated>2012-10-04T00:54:04Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== C++ ==&lt;br /&gt;
&lt;br /&gt;
Representations similar to the ones in Java are also seen in C++. However there are certain functions like &amp;quot;floor&amp;quot; that can be used in C++ for aiding in the right precision with decimal values for money quantities. The underlying data type for such cases is double. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
floor(double*21.32)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this function, decimals are not lost because the double type, with its precision being greater than or equal to 15 digits, is used as a compiler supported long long. The only thing to be careful of is multiplying and dividing such values.&lt;br /&gt;
The Zortech distribution has an in-built C++ Money Class in it.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class money {&lt;br /&gt;
    long dollars;&lt;br /&gt;
    int  cents;&lt;br /&gt;
    // ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
The Zortech C++ money class is an efficient implementation. This is due to the fact that the operators use integer arithmetic.&lt;br /&gt;
Not much overhead with maintaining the money class objects.&lt;br /&gt;
Some nice routines such as &amp;quot;flatten&amp;quot; are made available using the money class. An example of this implementation from is shown here.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Returns a money data item where the cents are&lt;br /&gt;
// rounded modulo &amp;quot;cents&amp;quot;. In this way cents can&lt;br /&gt;
// be stripped of money items when the currency&lt;br /&gt;
// does not have all the coins required to pay&lt;br /&gt;
// every posible quantity.&lt;br /&gt;
money flatten(const money&amp;amp; m, double cents, int rounding) {&lt;br /&gt;
    money temp;&lt;br /&gt;
    double c = floor(fabs(cents*money::SCALE())); // cents&lt;br /&gt;
    double r = fmod(m.m_money, c);            // remainder&lt;br /&gt;
    temp.m_money =&lt;br /&gt;
        (!rounding || (2.0* r &amp;lt;= c)&lt;br /&gt;
            ? m.m_money - r&lt;br /&gt;
            : m.m_money - r + c&lt;br /&gt;
        );&lt;br /&gt;
    return temp;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
The long datatype in the Zortech implementation cannot hold more than 10 decimal digits.&lt;br /&gt;
This money class implements most arithmetic operators, but it does not implement the multiply and divide operations. The user is left to implement these.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66916</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66916"/>
		<updated>2012-10-04T00:49:17Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== Java ==&lt;br /&gt;
&lt;br /&gt;
Apart from the implementations based on primitive data-types, BigDecimal data-type is used extensively for representation of money. A custom Money Class implementations can be used to provide a higher level of abstraction around the BigDecimal type. This makes it a more practical form of use.&lt;br /&gt;
Here is an example snippet that shows the use of BigDecimal to perform some basic calculations on Money values.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  import java.math.BigDecimal;&lt;br /&gt;
  import java.util.Currency;&lt;br /&gt;
&lt;br /&gt;
  public class MoneyCalculation {&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal fAmountOne, fAmountTwo;&lt;br /&gt;
  private static int DECIMALS = 2;&lt;br /&gt;
  private static final BigDecimal TWO = new BigDecimal(&amp;quot;2&amp;quot;);&lt;br /&gt;
  private static int ROUNDING_MODE = BigDecimal.ROUND_HALF_EVEN;&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getSum(){&lt;br /&gt;
    return fAmountOne.add(fAmountTwo);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  private BigDecimal getAverage(){&lt;br /&gt;
    return getSum().divide(TWO, ROUNDING_MODE);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the special cases of Java Money class implementation is found in Java Object-Relational Database Systems . Objects that are defined in Java are stored in FirstSQL/J as values in database columns. These are normal columns whose type is a Java class that is mapped to the appropriate database type internally.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Money Class&lt;br /&gt;
&lt;br /&gt;
Constructors: &lt;br /&gt;
Money(BigDecimal amt, String currency); &lt;br /&gt;
Money(double amt, String currency); Note: currency is a string name of a currency – ‘USD’, ‘Euro’, … &lt;br /&gt;
Methods: String getCurrency(); // get currency type string &lt;br /&gt;
String toString(); // get amount with standard formatting &lt;br /&gt;
BigDecimal decValue(); // get numeric amount &lt;br /&gt;
double doubleValue(); // get numeric amount&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
The BigDecimal representation has built-in rounding modes.&lt;br /&gt;
BigDecimal objects are immutable i.e. operations always return new objects, and do not modify the state of existing objects.&lt;br /&gt;
The use of Currency class in custom Money implementations allows users to represent money in any currency. Conversions from one currency to another can be easily done with the use of a Currency object.&lt;br /&gt;
An object oriented approach to Money allows encapsulation of methods such as adding, subtracting, negating money in one common implementation.&lt;br /&gt;
The FirstSQL/J inbuilt querying capabilities provide significant elegant methods to get information about Money classes.&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
Depending on the number of digits (&amp;lt;=9, &amp;lt;=18, &amp;gt;18), the corresponding types int, long, BigDecimal respectively are recommended for representation. This requires the user to be aware of what values the representation needs to take up.&lt;br /&gt;
Primitive types double and float carry small rounding differences.&lt;br /&gt;
Significant amount of logic is required for custom implementation of Mixed Money - different currencies being available. Certain inefficiencies of code can creep in if the user is not careful with some of the implementation details.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66900</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66900"/>
		<updated>2012-10-04T00:42:30Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Python - python-money ==&lt;br /&gt;
&lt;br /&gt;
The python-money implementation of storing money in an object is a relatively simple library written in Python. As such, it does not have as much functionality nor the complexities that many other implementations have.&lt;br /&gt;
The python-money module consists of two classes, Money and Currency. Much like other implementations, the Money object contains an amount and currency internally. The amount is specified as type Decimal, which allows storing the amount in a format similar to a float but without all of the precision issues that arise with using floats. When a Money object is created, either an identifier or Currency object is passed. Below is an example of the Currency data used for USD:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
CURRENCY['USD'] = Currency(code='USD', numeric='840', name='US Dollar', &lt;br /&gt;
                           countries=['AMERICAN SAMOA', 'BRITISH INDIAN OCEAN TERRITORY', 'ECUADOR', 'GUAM', &lt;br /&gt;
                                      'MARSHALL ISLANDS', 'MICRONESIA', 'NORTHERN MARIANA ISLANDS', 'PALAU', &lt;br /&gt;
                                      'PUERTO RICO', 'TIMOR-LESTE', 'TURKS AND CAICOS ISLANDS', &lt;br /&gt;
                                      'UNITED STATES MINOR OUTLYING ISLANDS', 'VIRGIN ISLANDS (BRITISH)', &lt;br /&gt;
                                      'VIRGIN ISLANDS (U.S.)'])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Within the namespace containing these classes, a default list of currency objects are created, indexed by their ISO4217 standard three character and number identifiers. This is what allows specifying currency by identifier rather than currency object. Unfortunately this limits the currency types to those statically defined in the source code. Additionally, since there is not a separate class to handle conversions, the exchange rate information is contained within the Currency object. This limits the way that Money objects can be converted.&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
Much like an integer value in Ruby, a Decimal has a theoretically infinite precision and maximum value.&lt;br /&gt;
As long as the user does not access the internal structure of the Money object directly, it behaves as an immutable object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
This implementation does not support an arbitrary number of decimal points&lt;br /&gt;
Since the currency conversion data is set within the Currency object itself&lt;br /&gt;
There is no locking present for the Currency object and there could be race conditions when users are simultaneously setting and getting exchange rates&lt;br /&gt;
The current implementation can only convert to and from the default currency type&lt;br /&gt;
Since this implementation can take multiple types of arguments as the monetary value but does not add methods to primitives for converting them to Money objects, type checking has to be done on arguments to determine how to handle them. This is something that we want to avoid as part of object oriented design.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66885</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66885"/>
		<updated>2012-10-04T00:37:34Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: /* Ruby - Money Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66880</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66880"/>
		<updated>2012-10-04T00:36:32Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: /* Ruby - Money Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66875</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66875"/>
		<updated>2012-10-04T00:35:36Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: /* Ruby - Money Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66872</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66872"/>
		<updated>2012-10-04T00:35:02Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66867</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66867"/>
		<updated>2012-10-04T00:33:16Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money - Skrien Chapter 6 gives an example of a class that can be used to represent money. But how is it done in real programs? Investigate, and report on the advantages and disadvantages of other approaches vs. Skrien's.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66862</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66862"/>
		<updated>2012-10-04T00:31:46Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: /* Representation using Primitive Data Types */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Primitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66860</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66860"/>
		<updated>2012-10-04T00:30:47Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Representation using Premitive Data Types ==&lt;br /&gt;
&lt;br /&gt;
'''Floating point Representation'''&lt;br /&gt;
&lt;br /&gt;
Money can be represented using floating point numbers. At a first look, this seems the most logical approach to handle money in programs. While floating-point data types are capable of representing extremely large positive and negative numbers and offer the equivalent of many decimal digits of precision, they are nonetheless inexact when it comes to representing decimal numbers. For example, 9.47 is stored as 9.479999542236328125. Thus performing arithmetic operations on such numbers further adds inaccuracy to the result.  &lt;br /&gt;
&lt;br /&gt;
'''Integer Representation'''&lt;br /&gt;
&lt;br /&gt;
A single Integer can be used to represent money value. Such an approach requires an assumption of an implicit decimal point.  For example $24.68 can be represented as 2468 considering the fact that for US Dollar representation, the decimal point is before the last two digits. This approach has the advantage of requiring less storage and accuracy in calculations. Drawbacks however crawl in when dealing with money belonging to different currencies having different implicit decimal points. &lt;br /&gt;
&lt;br /&gt;
'''Two Integers'''&lt;br /&gt;
&lt;br /&gt;
Two different integers can be used to represent the value of currencies with a fractional component.  The first part represents the main and next represents the decimal. The apparent  advantage of this approach is that displaying of the value will be simpler. Also the loss of accuracy in case of floating point data types can be avoided. &lt;br /&gt;
The code in this approach must be able to handle the overhead of calculations for carry over. The main component value has to be increased by 1 when the secondary value goes beyond 100. The overhead becomes very significant when applied to large scale calculations. Storage overheads must also be considered. Another significant drawback is that such a system cannot represent fractions of a fundamental unit of currency. For example, in the United States, gasoline prices are typically represented with a precision of tenths of a cent per gallon. Another example is the stock market, where until early in the 21st century, stocks were traded in units of sixteenths of a dollar (6.25 cents.)&amp;lt;ref name = stockreference /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Class Representation'''&lt;br /&gt;
&lt;br /&gt;
In Object Oriented programming languages, a class representation can be used for money. Such a class provides encapsulation and the users need not concern themselves as to how the positive and negative money is represented. Using objects to represent money can also help in determining logical errors like adding denominations in different currencies. Such errors might slip through if we use primitive representation of money.  &lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref name = stockreference&amp;gt;  http://www.infoplease.com/spot/stockdecimal1.html &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66677</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=66677"/>
		<updated>2012-10-03T23:23:19Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Representing money&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In most commercial applications we are required to create, manipulate and store currency values. The most important concerns about the representation and storage of money are accuracy and consistency. While manipulating currency values the precision to which calculations are done will affect the accuracy. Also while storing and retrieving the currency values, the correct storage types will ensure consistency. These concerns become even more significant in scenarios where we have multiple currencies and conversion operations between them. This page first discusses the merits of an approach described by Skrien and also critically analyses a number of other approaches and their relative advantages and disadvantages. This page first discusses the design approach described by Skrien. Most of the real program implementations are also inspired  from the same approach. We discuss some of them, their advantages and disadvantages in the following sections.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby - Money Gem ==&lt;br /&gt;
&lt;br /&gt;
A gem named Money provides an implementation similar in some aspects as one specified by Skrien. It represents monetary values as integers  in cents. Floating point rounding errors resulting from operations on decimals can be avoided using this implementation. The Money class uses Bank and Currency classes internally to define the attributes and behavior of a money object. It has built in ability to parse a money and currency string into corresponding Money/Currency object. The object of the type Money is immutable and conversions and operations result in  creation of a new object except that the currency identifier can be changed after the creation of the money object. Mentioned below is an example of the usage of the money class&lt;br /&gt;
&lt;br /&gt;
require 'money'&lt;br /&gt;
 &lt;br /&gt;
# 10.00 USD&lt;br /&gt;
money = Money.new(1000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
money.cents 	#=&amp;gt; 1000&lt;br /&gt;
money.currency  #=&amp;gt; Currency.new(&amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Comparisons&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;USD&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(100, &amp;quot;USD&amp;quot;)	#=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) == Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; false&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) != Money.new(1000, &amp;quot;EUR&amp;quot;)   #=&amp;gt; true&lt;br /&gt;
 &lt;br /&gt;
# Arithmetic&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) + Money.new(500, &amp;quot;USD&amp;quot;) == Money.new(1500, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) - Money.new(200, &amp;quot;USD&amp;quot;) == Money.new(800, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) / 5                 	== Money.new(200, &amp;quot;USD&amp;quot;)&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;) * 5                 	== Money.new(5000, &amp;quot;USD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Currency conversions&lt;br /&gt;
some_code_to_setup_exchange_rates&lt;br /&gt;
Money.new(1000, &amp;quot;USD&amp;quot;).exchange_to(&amp;quot;EUR&amp;quot;) == Money.new(some_value, &amp;quot;EUR&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Each money object is associated with a bank object which is responsible for the currency exchange. The Bank class is also an internal class to the Money class but it resides at the class level. This means that the Bank object follows the Singleton pattern in that only one Bank object exists across all of the currency. The purpose of the Bank is to maintain information related to currency values such that currency conversion can take place.This object uses a class name VariableExchange which performs the exchange. By default the class has no knowledge of exchange rates as they change dynamically.  The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites.Since the Bank class inherits from an interface, this allows for extensible money conversion schemes such as being able to 'scrape' data from the internet and use it to populate conversion ratios.&lt;br /&gt;
&lt;br /&gt;
bank = Money::Bank::VariableExchange.new&lt;br /&gt;
bank.add_rate(&amp;quot;USD&amp;quot;, &amp;quot;CAD&amp;quot;, 1.24515)&lt;br /&gt;
bank.add_rate(&amp;quot;CAD&amp;quot;, &amp;quot;USD&amp;quot;, 0.803115)&lt;br /&gt;
 &lt;br /&gt;
c1 = 100_00.to_money(&amp;quot;USD&amp;quot;)&lt;br /&gt;
c2 = 100_00.to_money(&amp;quot;CAD&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 USD to CAD:&lt;br /&gt;
bank.exchange_with(c1, &amp;quot;CAD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=1245150&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
# Exchange 100 CAD to USD:&lt;br /&gt;
bank.exchange_with(c2, &amp;quot;USD&amp;quot;) #=&amp;gt; #&amp;lt;Money @cents=803115&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attribute of the type Currency stores the currency the money is in.  The currency class by default has a table which is constant and has a list of known currencies. It specifies the three letter ISO4217 code for currency along with the monetary unit, their symbols and delimiters. The class also implements a CurrencyLoader class through which currency information can be loaded from specified locations. This allows the list to be easily manageable.  Below is an example of the USD currency information loaded by a default JSON configuration file.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;usd&amp;quot;: {&lt;br /&gt;
	&amp;quot;priority&amp;quot;: 1,&lt;br /&gt;
	&amp;quot;iso_code&amp;quot;: &amp;quot;USD&amp;quot;,&lt;br /&gt;
	&amp;quot;name&amp;quot;: &amp;quot;United States Dollar&amp;quot;,&lt;br /&gt;
	&amp;quot;symbol&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit&amp;quot;: &amp;quot;Cent&amp;quot;,&lt;br /&gt;
	&amp;quot;subunit_to_unit&amp;quot;: 100,&lt;br /&gt;
	&amp;quot;symbol_first&amp;quot;: true,&lt;br /&gt;
	&amp;quot;html_entity&amp;quot;: &amp;quot;$&amp;quot;,&lt;br /&gt;
	&amp;quot;decimal_mark&amp;quot;: &amp;quot;.&amp;quot;,&lt;br /&gt;
	&amp;quot;thousands_separator&amp;quot;: &amp;quot;,&amp;quot;,&lt;br /&gt;
	&amp;quot;iso_numeric&amp;quot;: &amp;quot;840&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
&lt;br /&gt;
The gem provides APIs for exchanging money from one currency to other. This can either be done by manually supplying exchange rates or by using exchange rates available at major banks and currency exchange websites. The implementation differs from Skrien's approach in one major aspect that is it does not have    &lt;br /&gt;
It is also worthwhile to mention a plugin named acts_as_money which makes it easier to work with money object. Inclusion of this gem allows the programmer to assume there are 2 columns in the database namely cents and currency.  Mentioned below is an example of the implementation. &lt;br /&gt;
&lt;br /&gt;
  acts_as_money&lt;br /&gt;
  money :rate, :cents =&amp;gt; :rate_in_cents, :currency =&amp;gt; :rate_currency&lt;br /&gt;
  money :discount, :cents =&amp;gt; :discount_in_cents, :currency =&amp;gt; false&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.&lt;br /&gt;
&lt;br /&gt;
r = Room.new :rate =&amp;gt; &amp;quot;100.00&amp;quot;&lt;br /&gt;
r.rate                            # returns &amp;lt;Money:0x249ef9c @currency=&amp;quot;USD&amp;quot;, @cents=10000&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Advantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The internal CurrencyLoader class allows for easily extensible currency information sources, even allowing currency information to be dynamically loaded&lt;br /&gt;
Due to the Bank implementation, additional class implementations can be loaded and changed out at runtime to allow for more complex, an potentially real-time, currency conversion information&lt;br /&gt;
The fact that the Currency and Bank classes are internal to theMoney class, the user doesn't have to know about their implementation&lt;br /&gt;
Ruby's dynamic nature allows for classes to be internal to Money, yet defined in separate files from the Money class, helping to keep the code modular from a developers point of view&lt;br /&gt;
The addition of to_money() methods to basic Ruby primitives makes it easier to create money from just about any type&lt;br /&gt;
Since Ruby supports arbitrarily large integer values, there is theoretically no upper bound to the amount of money that can be stored in a Money object&lt;br /&gt;
&lt;br /&gt;
'''Disadvantages'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Doesn't support multiple currency types in a single object&lt;br /&gt;
A Money object is immutable except in one way, ability to change currency type, which is an inconsistency that could easily have been avoided&lt;br /&gt;
While the addition of to_money() methods to primitive types makes some things easier, it adds the additional ability to incorrectly combine an object of type Money with one that is not&lt;br /&gt;
Since the Bank object is a singleton contained within the Moneyclass, any modifications to currency exchange rates require locking of the Bank object which could result in performance degradation if exchange rates are modified often&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=65737</id>
		<title>CSC/ECE 517 Fall 2012/ch6 1w49 aa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch6_1w49_aa&amp;diff=65737"/>
		<updated>2012-09-28T13:54:36Z</updated>

		<summary type="html">&lt;p&gt;Avyas2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; This page&lt;/div&gt;</summary>
		<author><name>Avyas2</name></author>
	</entry>
</feed>