|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Decimal/Single/...: which Numeric-type to use?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. But a 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! - 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. 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). This seems such a simple and basic need, but I just keep having troubles with it :-( What should I do/use? Another numeric datatype in my application? Another datatype in my Database?? Any help/hints/links/information would be really appreciated! Thansk a lot in advance, Pieter 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 Show quote "Pieter" wrote: > 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. But a > 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! > > - 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. > > > 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). > > This seems such a simple and basic need, but I just keep having troubles > with it :-( > > What should I do/use? Another numeric datatype in my application? Another > datatype in my Database?? > > Any help/hints/links/information would be really appreciated! > > Thansk a lot in advance, > > Pieter > > > 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... Show quote "guy" <g**@discussions.microsoft.com> wrote in message news:52297D53-1BC2-4196-908C-C31D3FA6BC77@microsoft.com... > 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 > 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/frlrfsystemdecimalclasstostringtopic3.asp hth guy Show quote "Pieter" wrote: > 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... > > > > "guy" <g**@discussions.microsoft.com> wrote in message > news:52297D53-1BC2-4196-908C-C31D3FA6BC77@microsoft.com... > > 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 > > > > > 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???? Show quote "guy" <g**@discussions.microsoft.com> wrote in message news:16801699-5A19-4AB5-84C0-4F2257EA6E4F@microsoft.com... > 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/frlrfsystemdecimalclasstostringtopic3.asp > > hth > > guy > 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. Show quote "Pieter" wrote: > 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???? > > "guy" <g**@discussions.microsoft.com> wrote in message > news:16801699-5A19-4AB5-84C0-4F2257EA6E4F@microsoft.com... > > 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/frlrfsystemdecimalclasstostringtopic3.asp > > > > hth > > > > guy > > > > > 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 Show quote "guy" <g**@discussions.microsoft.com> wrote in message news:FA5E0C37-A362-4579-B537-E6833183B1A8@microsoft.com... > 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. > 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 "Pieter" wrote: *** this wont work, the data type is decimal so you cant return a string ***> 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.##") > Show quote > End Get > > > "guy" <g**@discussions.microsoft.com> wrote in message > news:FA5E0C37-A362-4579-B537-E6833183B1A8@microsoft.com... > > 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. > > > > > 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 Show quote "Pieter" <pietercou***@hotmail.com> schreef in bericht news:Oto5eIZTGHA.4308@TK2MSFTNGP10.phx.gbl... > 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... > > > > "guy" <g**@discussions.microsoft.com> wrote in message > news:52297D53-1BC2-4196-908C-C31D3FA6BC77@microsoft.com... >> 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 >> > > 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... Show quote "Pieter" <pietercou***@hotmail.com> wrote in message news:uuj5lyYTGHA.4976@TK2MSFTNGP11.phx.gbl... > 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. But a > 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! > > - 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. > > > 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). > > This seems such a simple and basic need, but I just keep having troubles > with it :-( > > What should I do/use? Another numeric datatype in my application? Another > datatype in my Database?? > > Any help/hints/links/information would be really appreciated! > > Thansk a lot in advance, > > Pieter > Pieter wrote:
> Hi, Use the same type to process and store floating numbers, otherwise you> > 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. 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 Actually what happened is that Signgle (float) cannot represent 4.475> 4.4749999999999996D. So after inserting and selecting it from the database I > got another value than the original! 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 You should look into how the numbers are presented to the user. Have a> 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. look at ToString(format) and NumberFormatInfo. > What I need is: the users should only see the decimal part when needed, AND That can be done with Decimal, which can represent base10 without loss> my values have to be the same (no 4.475 to 4.474999999999 changements). 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 floating-point precision numbers are not "reals" as in math, they do not> with it :-( have infinite precision, that's the problem here. > What should I do/use? Another numeric datatype in my application? Another Decimal all-round if your *really* need the precision (not just as> datatype in my Database?? 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. -- Helge Jensen mailto:helge.jen***@slog.dk sip:helge.jen***@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=-
Other interesting topics
Overlaping structure
Process.Start() throws an error saying "Setup error: failed to load resources from resource file Ple WebRequest & proxy problem are System.Data.SqlClient.SqlConnection thread safe? can many threads share a System.Data.SqlClient. Wrong size when restoring form that has no WS_DLGFRAME style flag set |
|||||||||||||||||||||||