Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimal.ToString issue

Status
Not open for further replies.

SHelton

Programmer
Jun 10, 2003
541
GB
Can anybody tell me why the following conversion of a decimal to a string is displayed differently on my development machine (W2K Pro) and the Terminal Server it is to run from (2003 Server) ? I am taking a decimal value from a datatable and inserting it into a text box, like:

TextBox1.Text = dt.Rows(0).Item("Value").ToString

In W2K, the text box displays "1", but in 2003 Server it displays "1.00". I have checked the regional settings for both machines and they are identical. The 2003 Server displays a zero for each decimal place, whereas W2K removes the unnecessary zeros.

Any ideas appreciated !
 
Hmmm... I don't know about this because I only have W2K pro here but i'd replace :

TextBox1.Text = dt.Rows(0).Item("Value").ToString

with

TextBox1.text = format(dt.rows(0).item("Value"), "d.##")

Let me know how you get on!!!



Stay Blue, Bob. x
 
If you would like to stay "pure" framework, you can pass your format in the ToString method

TextBox1.Text = dt.Rows(0).Item("Value").ToString("####.99")


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hmmm...

John, that doesn't even remotely work!!! LOL!



Stay Blue, Bob. x
 
It does work assuming that the Value comes out of the item AS Decimal and you correct the format to put 00 where the 99 is.

Dim decw As Decimal = 1.0
MessageBox.Show(decw.ToString("####.00"))

Decimal.ToString Method (String)
Visual Basic

Converts the numeric value of this instance to its equivalent String representation, using the specified format.

[Visual Basic]
<Serializable>
Overloads Public Function ToString( _
ByVal format As String _
) As String


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks for your efforts guys.

The Decimal.ToString(&quot;##.00&quot;) would solve my problem if all my decimals were to 2 decimal places, and also if all my datarow columns were decimals. However, in my iteration through through the text boxes in order to dislay values, some values will be strings, some decimals, some integers etc.

The result I am trying to achieve is as per W2K displays - if a decimal (e.g. '2.0') has no decimal places it is displayed as a whole number (i.e. '2'), otherwise it is displayed to 2 decimal places.

I guess the most confusion on my part is why they are displaying okay in W2K but differently in W2003 Server.
 
I Shelton

Are you running the same .net framework version in both systems?
I had some problems with decimals in the new version is more strict with types.

Hope it helps.

Mario
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top