Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two decimal places for decimal/money field

I have a table with money field in my database. I have created entity and created decimal property for that money field. When the value of that field is displayed on My MVC3 view, It has four zeros 0000 after decimal like this : 5489.0000. I want to show only two 00 or decimal places. How can I fix it. Why it is showing four decimal places even I declared property as decimal.

Please suggest.

like image 564
DotnetSparrow Avatar asked May 20 '11 16:05

DotnetSparrow


People also ask

Is money always to 2 decimal places?

Not all currencies across the world have two decimal places. Some have zero decimal places (e.g. Japanese yen), some have three decimal places (the dinar in many countries), and in Madagascar the minor unit is one fifth of the major unit so currency would be written to one decimal place.

What is the decimal place for money?

United States (U.S.) currency is formatted with a decimal point (.) as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation.

Can money have more than 2 decimal places?

Most currencies have two decimals. Some currencies do not have decimals, and some have three decimals. For example: 10 GBP: GBP has two decimals, so in minor units submit an amount of 1000.

What is a decimal with 2 decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.


2 Answers

The SQL Server money datatype internally is a 64-bit integer with an implied scale of 4 decimal places. To quote Books Online, it is accurate "to ten-thousandsth of a currency unit." It is, the rough equivalent of a decimal(19,4).

The reason for the scale of 4 rather than 2 is to maintain precision in the results of arithmetic. Your ordinary currency value has a scale of 2 (e.g. $3.27) Multiplication or division of two numbers scaled to 2 decimal places gives a results that is precise to 4 decimal places: 9.23 divided by 3.27 yields a result of 2.82262996941896 (approximately). You can carry the result to whatever accuracy (number of decimal places) you desire. However, the result is only precise to 4 decimal places (2.8226) as the original values were only precise to 2 decimal places. That measurement is precise to within 1/2 of the smallest unit specified (+/- 0.005).

But I digress.

As a result of a SQL Server money value having an implied scale of 4, ADO.Net converts the value to a System.Decimal with a scale of 4. And since System.Decimal tracks scale, when you convert it to string, you get 4 decimal places.

To get fewer, you can

  • Round it before conversion, using the appropriate Decimal.Round() overload, or
  • Format it as desired (eg. (3.27M).ToString("0.00") ;.

Hope this helps.

This program:

namespace Sandbox
{
  using System ;
  class Program
  {
    static void Main( string[] args )
    {
      decimal pi     = (decimal) Math.PI ;
      string  piText = pi.ToString("0.00");
      Console.WriteLine("PI to 2 decimal places is {0} one way, and {1:0.00} another" , piText , pi ) ;
      return;
    }
  }
}

Produces what you'd expect:

PI to 2 decimal places is 3.14 one way, and 3.14 another

Cheers,

N.

like image 149
Nicholas Carey Avatar answered Sep 24 '22 05:09

Nicholas Carey


You have to format the string.

One thing you can do if it money you want to display is:

static void Main () 
{
    decimal x = 0.999m;
    decimal y = 9999999999999999999999999999m;
    Console.WriteLine("My amount = {0:C}", x);
    Console.WriteLine("Your amount = {0:C}", y);
}

}

OUTPUT: Output

My amount = $1.00
Your amount = $9,999,999,999,999,999,999,999,999,999.00

the {0:C} is the currency Format

Hope this helps!

like image 31
NicoTek Avatar answered Sep 26 '22 05:09

NicoTek