473,418 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Odd behaviour with Decimal.ToString

Can anyone explain why the following happens?

\\\
Dim d1 As Decimal = CDec("100")
Dim d2 As Decimal = CDec("100.00")

MsgBox(d1.ToString) 'displays "100"
MsgBox(d2.ToString) 'displays "100.00"

MsgBox(d1 = d2) 'displays "True"
///

How are the Decimal variables remembering the fact that I provided the two
decimal places for d2, but not for d1? I can't see any properties of the
Decimal structure that would allow it to do this...

Thanks,

--

(O)enone
Nov 23 '05 #1
7 2648
I think it has to do with having . set as decimal seperator and , grouping
symbol in your regional settings
Because with me 100 = 100.00 = false
but 100 = 100,00 = true because I've my decimal seperator = , and grouping
symbol = .
If I set . as decimal seperator and , as grouping symbol then 100 = 100.00
= true
and 100 = 100,00 = false

hth

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Oenone" <oe****@nowhere.com> schreef in bericht
news:OW*************@TK2MSFTNGP10.phx.gbl...
Can anyone explain why the following happens?

\\\
Dim d1 As Decimal = CDec("100")
Dim d2 As Decimal = CDec("100.00")

MsgBox(d1.ToString) 'displays "100"
MsgBox(d2.ToString) 'displays "100.00"

MsgBox(d1 = d2) 'displays "True"
///

How are the Decimal variables remembering the fact that I provided the two
decimal places for d2, but not for d1? I can't see any properties of the
Decimal structure that would allow it to do this...

Thanks,

--

(O)enone

Nov 23 '05 #2
Peter Proost wrote:
I think it has to do with having . set as decimal seperator and ,
grouping symbol in your regional settings


Ah sorry, I should have been clearer with that -- I do indeed have "." as
decimal and "," as thousands separators (UK regional settings).

But that wasn't the question I was asking -- the question was how and why
the Decimal variable stores the fractional part of the value when all of the
fractional digits were zero. How does the Decimal variable distinguish
between me storing "100.00" and "100"? I would have expected
Decimal.ToString to return "100" in both cases, just like a Double variable
does.

--

(O)enone
Nov 23 '05 #3
It seems normal behavior to me, but to the how and why I can't realy give
you an answer. To only thing I could find in the msdn help is:

Decimal.ToString Method (String) [Visual Basic]

Remarks
If format is a null reference (Nothing in Visual Basic) or an empty string,
the return value of this instance is formatted with the general format
specifier ("G").

Hth Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

"Oenone" <oe****@nowhere.com> schreef in bericht
news:#p**************@tk2msftngp13.phx.gbl...
Peter Proost wrote:
I think it has to do with having . set as decimal seperator and ,
grouping symbol in your regional settings
Ah sorry, I should have been clearer with that -- I do indeed have "." as
decimal and "," as thousands separators (UK regional settings).

But that wasn't the question I was asking -- the question was how and why
the Decimal variable stores the fractional part of the value when all of

the fractional digits were zero. How does the Decimal variable distinguish
between me storing "100.00" and "100"? I would have expected
Decimal.ToString to return "100" in both cases, just like a Double variable does.

--

(O)enone

Nov 23 '05 #4
Oenone wrote:
How are the Decimal variables remembering the fact that I provided
the two decimal places for d2, but not for d1? I can't see any
properties of the Decimal structure that would allow it to do this...


From the help:-
"Decimal variables are stored as signed 128-bit (16-byte) integers scaled by
a variable power of 10. The scaling factor specifies the number of digits to
the right of the decimal point"

So you have a case of scaling factor being 0 or 2.

Andrew
Nov 23 '05 #5
Andrew Morton wrote:
From the help:-

[...]

Ah, that'll teach me to read before posting questions. :)

Many thanks for your reply, that explains what I'm seeing.

--

(O)enone
Nov 23 '05 #6

Oenone wrote:
Can anyone explain why the following happens?

\\\
Dim d1 As Decimal = CDec("100")
Dim d2 As Decimal = CDec("100.00")

MsgBox(d1.ToString) 'displays "100"
MsgBox(d2.ToString) 'displays "100.00"

MsgBox(d1 = d2) 'displays "True"
///

How are the Decimal variables remembering the fact that I provided the two
decimal places for d2, but not for d1? I can't see any properties of the
Decimal structure that would allow it to do this...
This was pretty interesting! For the short answer, set a breakpoint on
a msgbox and note the difference in output here (GetBits is what we use
when we find that BitConverter.GetBytes doesn't accept a Decimal...)

?system.decimal.getbits(d1)
{Length=4}
(0): 100
(1): 0
(2): 0
(3): 0
?system.decimal.getbits(d2)
{Length=4}
(0): 10000
(1): 0
(2): 0
(3): 131072

Now the reason .Equals (which is what = gets compiled to) returns true
is because these two values do both represent the same value (exactly
100) but as you can see, internally they have different
representations!
From the help:

A decimal number is a signed, fixed-point value consisting of an
integral part and an optional fractional part. The integral and
fractional parts consist of a series of digits that range from zero to
nine (0 to 9), separated by a decimal point symbol.

The binary representation of an instance of Decimal 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.


So without poking around too much, we can surmise that CDec("100") gets
you a decimal which is stored as 100 * 10^0, but CDec("100.00") gets
you a decimal which is stored as 10000 * 10^-2. And a plain vanilla
..ToString just dumps out the internal representation.

The lesson? Always specify a format for outputting Decimals, I suppose.

--
Larry Lard
Replies to group please

Nov 23 '05 #7
Larry Lard wrote:
This was pretty interesting!
Thanks very much for your detailed reply, always nice to find more about the
internal workings of VB.NET.
The lesson? Always specify a format for outputting Decimals, I
suppose.


The code I was trying to write was a generic piece of code that builds XML,
and ultimately compares the XML as a string against another piece of XML, so
I couldn't put any formatting into the ToString() call as I didn't know what
format to use. I resolved it by converting the Decimal to a Double, and then
calling the ToString() method on that. That returns the numbers with no
unnecessary decimal places regardless of how the value was originally
specified.

--

(O)enone

Nov 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a...
3
by: Xavier Wargny | last post by:
Hi all and each! Since july, I'm working with dot NET technology. Confident with brand new tools it gave, I decided to use that new decimal type (related with the Database type...). After many...
4
by: Ron | last post by:
Greetings, int i = 1, j = 6; double k = (double)i/j; Console.WriteLine(k.ToString()); Console.WriteLine(string.Format(k.ToString(), "0.00")); both yield 0.166666666666667 how can I make...
2
by: Piotr | last post by:
Hi I'm working with Datagrid containg decimal (editable) numbers. The user-entered numbers are read by decimal.Parse() method. However, Datagrid formats some decimals as "1", but some as (eg)...
2
by: Scott | last post by:
I want to display a decimal as a currency value for instance £65.00 or £58.47, but at the moment the code doesn't seem to work correctly. Can I declare this variable to be shown with 2 decimal...
5
by: Patrick Sullivan | last post by:
I was having hell trying to get values like 6.50 to return two ints, a = 6 and b = 50. The data is from a program that uses nn.nn for a time format in hours and minutes. But 6.50 in decimal would...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
10
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole...
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.