bman9111

asked on

what would be an example of a decimal variable?

I tried 1.23 and it throws an error.

I tried 1.23 and it throws an error.

Last Comment

Hi bman9111,

A Decimal is actually an Integer. More specifically, a 128-bit (16-byte) integer. See here -> http://msdn2.microsoft.com/en-us/library/xtba3z33.aspx

For your example of 1.23, you'd use a Double variable type.

Regards,

Wayne

A Decimal is actually an Integer. More specifically, a 128-bit (16-byte) integer. See here -> http://msdn2.microsoft.com/en-us/library/xtba3z33.aspx

For your example of 1.23, you'd use a Double variable type.

Regards,

Wayne

ASKER

so for writing accounting\math programs I should use double variable vs decimal?

Yes. Only use Decimal or Integer where whole numbers are required.

For accounting programs in small or medium applications you can use just int.

Max value of int is 2,147,483,647 that enough for such apps.

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

For math routines where high precision is required - decimal is best. Also Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors.

Max value of int is 2,147,483,647 that enough for such apps.

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

For math routines where high precision is required - decimal is best. Also Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors.

ASKER

I am doing a lot of accounting and percentage calculations so which variable type should I use. Right now I am using double but I am not 100% certain if that is the best way.

ASKER

please keep in mind there will be a lot of calculations in this program. Currently I am using double with the math.round.

Decimal is your choice, because it have most precision.

ASKER

lol in other posting I am being told double. Im so confussed

Where is it?

Try by yourself to declare 2 variables - double and decimal, both with 30 dihits after decimal point.

And you get:

Dim dec As Decimal = 1.1234567890123456789012345679D - 28 digits

Dim dob As Double = 1.1234567890123457 - 16 digits

Feel the difference...

Try by yourself to declare 2 variables - double and decimal, both with 30 dihits after decimal point.

And you get:

Dim dec As Decimal = 1.123456789012345678901234

Dim dob As Double = 1.1234567890123457 - 16 digits

Feel the difference...

ASKER

View this solution by signing up for a free trial.

Members can start a 7-Day free trial and enjoy unlimited access to the platform.

ASKER

When should a double be used then?

Double can be used for big values with floating point or for some compatibility reasons.

.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K

Questions

--

Followers

--

Top Experts

Get a personalized solution from industry experts

TRUSTED BY

decimal myMoney = 300.5m;

Without the suffix m, the number is treated as a double, thus generating a compiler error.

The integral types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal, without the suffix, as follows:

decimal myMoney = 300;