0

UPDATE

It's so simple...

When I try to convert the value $ 1.50 from a textbox to a decimal variable, like this:

decimal value = Convert.ToDecimal(textbox1.text.SubString(1));

OR

decimal value = Decimal.Parse(textbox1.text.SubString(1));

I get this result: 1.5.
I know that 1.5 and 1,50 worth the same. But I want to know if it's possible to have two digits after the dot on a decimal variable.

I want to have this as result: 1.50 instead of 1.5 even if these two values worth the same...

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • Can you please explain more what you are having trouble with, I don't understand what you want based on your statement "But I want this: 1,50 / 1.50. " Please explain more what values you are getting out, what values you expect to get out, and how you are viewing those values. – Scott Chamberlain May 15 '14 at 21:33
  • @ScottChamberlain It's right there o.O . I want two numbers after the dot/comma like: $ *1,50 / 1.50*, but I'm getting this: $ *150 / 1.5*. – PlayHardGoPro May 15 '14 at 21:37
  • See this question about c# currency formatting. [Stack overflow currency formatting question][1] [1]: http://stackoverflow.com/questions/1071273/currency-formatting – Richard Vivian May 15 '14 at 21:37
  • 4
    You are talking about displaying `value` *but you never show your code for how you display it*. decimal has no concept of trailing zeros, it is all in how you formatted the `.ToString()` or equivalent when you go back from a decimal to string. Are you storing it in the database as a string or as a decimal? When in `decmal` form think of it as a word problem. "one point five", how many zeros are after "one point five"? – Scott Chamberlain May 15 '14 at 21:38
  • @ScottChamberlain Ok. Could you take a look at the updated question explanation? Hope I could be clear. I'll get the value from a *TextBox* assign it's value into a *decimal* variable type, then save it into a *database decimal field type*. – PlayHardGoPro May 15 '14 at 21:53
  • 1
    Still not sure what the question is. There are no trailing 0s in a decimal type, so what are you trying to do? The parse returning 1.5 is correct, thats all you need. As far as the decimal type is concerned, that's all you need (its the same as 1.50, 1.500, etc.) – BradleyDotNET May 15 '14 at 21:57
  • I see no code with a comma – codenheim May 15 '14 at 21:59
  • @BradleyDotNET I know, but isn't there a way to store exactly the *1.50* ? I know when talking about value it worth the same. But Is it possible, to store with two digits after the dot ? – PlayHardGoPro May 15 '14 at 21:59
  • 3
    The "format" of the decimal value in the database **should not** be important here. You only format it when you display the value to the end user. – LarsTech May 15 '14 at 22:03
  • @LarsTech Finally !! Got ya, so if I store the value as *1.5* instead of *1.50*, how may I change it: FROM *1.5* to *1.50* when I'll display it to the end user ? – PlayHardGoPro May 15 '14 at 22:05
  • What about this? http://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa – Chuck Conway May 15 '14 at 22:06
  • 2
    You already know how from your original post: `string num = string.Format("{0:0.00}", yourDecimal);` – LarsTech May 15 '14 at 22:07
  • @LarsTech +1 Ok', I understood now... CaseClosed And Thank you! – PlayHardGoPro May 15 '14 at 22:08

2 Answers2

2

I want to have this as result: 1.50 instead of 1.5 even if these two values worth the same..

You have 1.50 or 1.500 or 1.5000. all depending on how you decide to format it / print it.

Your decimal value is stored in floating point format. How many decimal points you see is about output, not storage (at least until you reach the limit of the precision of the particular binary format, and 2 decimal places is nowhere close). A C# Decimal stores up to 29 significant digits.

See this reference. It gives an example of a currency format. It prints something like:

My amount = $1.50

But, you aren't storing a $ sign..., so where does it come from? The same place the "1.50" comes from, it is in your format specifier.

http://msdn.microsoft.com/en-us/library/364x0z75.aspx

Console.WriteLine("My amount = {0:C}", x); 
var s = String.Format("My amount = {0:C}", x); 

It is no different than saying, how do I store 1/3 (a repeating decimal)?

Well, it isn't 0.33, but if I only look at the first 2 digits, then it is 0.33. The closer i look (the more decimal places I ask for in the format), the more I get. 0.33333333333333... but that doesn't equal 0.330

codenheim
  • 20,467
  • 1
  • 59
  • 80
  • Ok, so I only may "choose" how many zeros to display when it comes to *strings*, right ? When we talk about save it on the database into a *decimal field type*, I can **only** save it as *1.5*. Is that right ? – PlayHardGoPro May 15 '14 at 22:10
  • Yes, but it's not _really_ saved as 1.5. It is saved as an unreadable binary format. ToString converts it based on the format you send it (same for stringbuilder and string.format as well). – FastAl May 15 '14 at 22:12
  • 1
    @PlayHardGoPro - Yes, and no. Databases allow you to specify precision per field, so you can actually override that. In Oracle I might say NUMBER(10,2). But in C#, it is a 64 or 128 bit floating point variable. The "choice" of zeroes is about format when you stringify or print it out. – codenheim May 15 '14 at 22:13
1

You're confusing storage of the numeric value with rendering it as a string (display).

decimal a=1.5;
decimal b=1.50;
decimal c=1.500;

In memory: the zeros are kept to keep track of how much precision is desired. See the link in the comment by Chris Dunaway below.

However, note these tests:

(a==b) = true
(b==c)=true

Parsing ignores the trailing zeros, so your example one creates them, then they're ignored, as they're mathmatically irrelevant.

Now how you convert to string is a different story: a.ToString("N4") returns the string "1.5000" (b. and c. the same)

a.ToString("N2") returns the string "1.50"

As the link in the comment explains, if you just to a.ToString, trailing zeros are retained.

If you store it in a database column as type 'decimal', it might be a different story - I haven't researched the results. These are the rules that .Net uses and while the databases might use different rules, these behaviours often follow official standards, so if you do your research you might find that the database behaves the same way!

The important thing to remember is that there is a difference between the way numbers are stored in memory and the way they are represented as strings. Floating point numbers may not retain trailing zeros this way, it's up to the rules of the in-memory storage of the type (usually set by standards bodies in very specific, detailed ways).

FastAl
  • 6,194
  • 2
  • 36
  • 60
  • NICE !! I'm so stupid =s So I can change the way it's displayed to the user (as a string). But there's no way to change the way it's storage into a decimal variable or saved into a a database decimal field type. As it automatically removes the traillimg zeros. This is it ? – PlayHardGoPro May 15 '14 at 22:13
  • Actually, in .Net, the decimal data type does preserve the trailing zeroes, even if they are insignificant. See [this link](http://www.blackwasp.co.uk/DecimalTrailingZeroes.aspx). This is also easily demonstrated in code: `decimal d = 1.500000m; Console.WriteLine(d)` which prints `1.500000` – Chris Dunaway May 16 '14 at 15:55