Only show decimals if there are any
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
So I found a way of doing this. If you use Decimal.ToDouble it seems to do the job :)
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
I am not sure which version of .Net you are using but in VS 2010(.Net 4.0) in a winforms app, this seems to be the default behaviour. I know if I want to always show the decimal part (500.00), that is when I need to go the ToString route. I just tested with a listbox and with textboxes, and I get no decimal points if the decimal is a whole number.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
-
From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
Hear, Hear.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
-
From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
-
So I found a way of doing this. If you use Decimal.ToDouble it seems to do the job :)
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
Etienne_123 wrote:
ONLY show decimals
Show it where? :confused:
-
From a general programming point of view you should never change your data type to satisfy display requirements, if by changing that datatype you remove precision. Converting to a double will lose some precision. Although very little precision is generally lost - if you are performing financial calculations this can cause a problem further on down the line when figures do not match. So it really is better to stick with decimal and then to format it for display - otherwise you may be asking for trouble later when the FD asks "Why don't the totals match?"...
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
I`m not too concerned with precision. At the moment it works perfectly - If I do have decimals it keeps the decimals and if I don't then it shows the whole number. I just tested this by creating a random decimal of 4.4567, converted it to a double and the value stayed the same
-
Hi Is it possible to have a variable ONLY show decimals if it does have decimal values? E.g. if the value comes back grom the database as 500 then instead of showing 500.00, it should just show 500. If it comes back as 500.55, then it should show 500.55. Please note that I do not want to use string formatting here as I want to retain the decimal type for sorting purposes.
Etienne_123 wrote:
I do not want to use string formattin
What is displayed on screen is a string that is currently using the default formatting, so doing a custom string format is the correct way. The sort should be done on the real values regardless of how it's displayed IMO, this way therer is no issue.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)