er, it depends
are the calculations that you are performing financial style calcs where
precision is everything? if so use Decimal. or are they scientific calcs
where you have a wide range of numbers that need ot be accurate to a a given
nummber of digits? (eg calculation of stresses in an engineering project) in
this case use Double. I wouldnt use Single unless... it is a leagacy app
being converted or you have REALLY big arrays and are tight on memory
dont worry about the formatting, that is trivial comapred with getting the
data right, you can sort it out later.
hth
guy
> Hi,
>
[quoted text clipped - 25 lines]
>
> Pieter
Pieter - 22 Mar 2006 10:22 GMT
Precision is indeed everything. Most of the time it are prices, weights,
heights etc. So it has to be exact.
And I do worry about the formatting, because when I show the decimal value
in a Textbox, it shows all the zeros after the decimal, and I don't want it
to do it. Especially in some cases where it contains most of the time whole
numbers (1, 2, 3, ...) and rarely 1.001 etc. So I don't want the user to be
seeing the whole time 1.0000, 2.0000, 3.0000, etc.
Basicly: I need exact precision, but without the zeros at the end.
How should I do that? there isn't a Format-property on the textbox...
> er, it depends
> are the calculations that you are performing financial style calcs where
[quoted text clipped - 12 lines]
>
> guy
guy - 22 Mar 2006 10:45 GMT
look at the overloads of Decimal.ToString
this one may help...
(it is the online version of Help)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemdecimalclasstostringtopic3.asp
hth
guy
> Precision is indeed everything. Most of the time it are prices, weights,
> heights etc. So it has to be exact.
[quoted text clipped - 25 lines]
> >
> > guy
Pieter - 22 Mar 2006 10:58 GMT
Thanks, but this means that I have to change manually every numeric property
of my objects to a string? And that I can't anymore DataBind my controls
directly to the propertys of my objects? It seems a little bit a werid
solution for such a problem.
Why does a Single threats the trailing zeros right (cuts them off) but
doesn't have ap recision, and does a decimal the opposite?
Isn't there somehow a datatype that combines the two? I can't be the only
one with this problem????
> look at the overloads of Decimal.ToString
>
[quoted text clipped - 6 lines]
>
> guy
guy - 22 Mar 2006 11:21 GMT
another thing to consider...
in one project i have developed, i built a set of structuers - Distance,
Area, volume, Weight etc. which wrapped a double (you could equally well use
a decimal) with appropriate overloads and formatting. It sounds a lot of work
but more than paid for itself in functionality and ensuring correct variable
usage.
> Thanks, but this means that I have to change manually every numeric property
> of my objects to a string? And that I can't anymore DataBind my controls
[quoted text clipped - 16 lines]
> >
> > guy
Pieter - 22 Mar 2006 11:28 GMT
Ok, thanks for the help on this issue.
So if I am right I should define my propertys like this:
Public Property FraisEmballageOriginal() As Decimal
Get
Return m_decFraisEmballageOriginal.ToString("0.##")
End Get
> another thing to consider...
> in one project i have developed, i built a set of structuers - Distance,
[quoted text clipped - 5 lines]
> variable
> usage.
guy - 22 Mar 2006 12:19 GMT
Pieter,
the key is to separate what you display to the operator and the actual value
in your classes.
use the data type of your choice in the class, but build a formatting method
to present it to the operator that does not interfere with applicationss
internals
> Ok, thanks for the help on this issue.
> So if I am right I should define my propertys like this:
[quoted text clipped - 4 lines]
>
> Return m_decFraisEmballageOriginal.ToString("0.##")
*** this wont work, the data type is decimal so you cant return a string ***
> End Get
>
[quoted text clipped - 7 lines]
> > variable
> > usage.
Cor Ligthert [MVP] - 22 Mar 2006 10:55 GMT
Pieter,
I have the idea that you do not use precise but rounded decimal values.
Be aware that it uses Banking roundings as default. In version 2005 you can
as well use other roundings (as standard is used in the Benelux by
instance). I could not find the information on MSDN, so have a look for
yourself.
Cor
> Precision is indeed everything. Most of the time it are prices, weights,
> heights etc. So it has to be exact.
[quoted text clipped - 26 lines]
>>
>> guy
I did some test:
I used single in my application, and Float, Real, Decimal and Numeric in my
database.
The combination Single-float/Real works great, and give mle the exact
results that I wanted. Should I use this? Or is there some problem that I'm
not seeing?
It doesn't round the precision, and it cuts off the trailing zeroes...
> Hi,
>
[quoted text clipped - 26 lines]
>
> Pieter
> Hi,
>
> I'm having some troubles with my numeric-types in my VB.NET 2005
> application, together with a SQL Server 2000.
>
> - I first used Single in my application, and Decimal in my database.
Use the same type to process and store floating numbers, otherwise you
will multiply loss of precision.
Single numbers are "float", and uses base 2, that is it can represent
multiples of halves without precision loss (upto the precision limits).
Decimal is base10 and can represent decimal-numbers without loss (upto
the precision limit).
> Single with value 4.475 was converted to a Decimal with value
> 4.4749999999999996D. So after inserting and selecting it from the database I
> got another value than the original!
Actually what happened is that Signgle (float) cannot represent 4.475
without loss, it becomes a bit-pattern which represents
4.4749999999999996. So the conversion to decimal does not loose
precision, the use of float to represent base10 numbers does.
If you format 4.475 as: (4.475).ToString("##.###") you *will* get 4.475,
since the presentation will do rounding.
> - So I used also the Decimal in my application, but now all my numbers are
> formatted with all the decimals: when my value is "1", it shows "1.0000". A
> single value roudns it nicely to 1, but the decimal value shows the whole
> decimal part, even when it isn't needed.
You should look into how the numbers are presented to the user. Have a
look at ToString(format) and NumberFormatInfo.
> What I need is: the users should only see the decimal part when needed, AND
> my values have to be the same (no 4.475 to 4.474999999999 changements).
That can be done with Decimal, which can represent base10 without loss
of precision. beware you you may stil inadvertently generate numbers
which does *not* have representation without loss in base 10, for
example by division.
> This seems such a simple and basic need, but I just keep having troubles
> with it :-(
floating-point precision numbers are not "reals" as in math, they do not
have infinite precision, that's the problem here.
> What should I do/use? Another numeric datatype in my application? Another
> datatype in my Database??
Decimal all-round if your *really* need the precision (not just as
4.475), otherwise just use the same in app and databse. And learn to
format the numbers presented to the users, have a look at ToString with
formatting arguments and the string-conversions in string.Format.
Whatever you do, dont start using strings to represent numbers in your
calculations and interface, that won't help you.

Signature
Helge Jensen
mailto:helge.jensen@slog.dk
sip:helge.jensen@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-