Avatar of bman9111
bman9111

asked on 

decimal error

what would be an example of a decimal variable?

I tried 1.23 and it throws an error.

.NET Programming

Avatar of undefined
Last Comment
igor_alpha
Avatar of igor_alpha
igor_alpha

If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:

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;
Avatar of Wayne Taylor (webtubbs)
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
Avatar of bman9111
bman9111

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.
Avatar of igor_alpha
igor_alpha

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.
Avatar of bman9111
bman9111

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.
Avatar of bman9111
bman9111

ASKER

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

Decimal is your choice, because it have most precision.
Avatar of bman9111
bman9111

ASKER

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

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...
ASKER CERTIFIED SOLUTION
Avatar of igor_alpha
igor_alpha

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
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.
See Pricing Options
Start Free Trial
Avatar of bman9111
bman9111

ASKER

When should a double be used then?
Avatar of igor_alpha
igor_alpha

Double can be used for big values with floating point or for some compatibility reasons.
.NET Programming
.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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo