Decimals

  • Thread starter Thread starter karim
  • Start date Start date
K

karim

Hello All,
why is this code is not showing the result in this format: 0.00 and
showing it as only 0

Private Sub btn1_Click
Debug.Print(Format$(Rnd() * 100, "0.00"))

Dim d As Double = Math.Round(2250.0, 3)

txt2.Text = txt1.Text \ d
 
well, i thought that the Debug.print line apply to what is under it. and yes,
i'm talking about txt2. it always show 0, but i'm trying to get it to show
0.1
 
karim said:
Hello All,
why is this code is not showing the result in this format:
0.00 and showing it as only 0

Private Sub btn1_Click
Debug.Print(Format$(Rnd() * 100, "0.00"))

Dim d As Double = Math.Round(2250.0, 3)

txt2.Text = txt1.Text \ d

Please enable Option Strict first. You must become aware of the data types
you are dealing with. Otherwise you will get unexpected results forever.

Second, you must convert strings to numeric values. You can not calculate
with strings.

"\" is an Integer division operator, that means, you can not divide floating
point values with it. Use "/" for floating point divisions.

A Double object has a ToString method that can be used easily.

Why do you want to round 2250 to 3 decimal places? It already has 0
decimal places.


Armin
 
Hello Armin Zingler
thank you very much. I didn't have option strict on. and
i don't want to round to the 2250 to 3 decimal places, what i want is to
divide whatever in txt1 by 2250 and display it in txt2 and #.###
 
well, I tried this:

txt2.Text = CStr(CDbl(txt1.Text) / 2250)

but it's showing lots of decimals. how can i get it to show only 2 or 3
decimals?

and thanks partice for leting me see that i didn't need the Debug.print or
the Math.Round.

the code works, i just needed the decimals. and now that i have the
decimals, they are too much...
 
karim said:
well, I tried this:

txt2.Text = CStr(CDbl(txt1.Text) / 2250)

but it's showing lots of decimals. how can i get it to show only 2 or
3 decimals?

and thanks partice for leting me see that i didn't need the
Debug.print or the Math.Round.

the code works, i just needed the decimals. and now that i have the
decimals, they are too much...

As Armin wrote, you can use .ToString:

txt2.Text = ((CDbl(txt1.Text) / 2250)).ToString("0.000")

You can find out about the different things .ToString can do by googling for

vb.net tostring format

HTH

Andrew
 
This should give you two places to the right of the decimal

Dim Result As Double = CDbl(txt1.Text) / 2250
txt2.Text = Result.ToString("##0.00;(##0.00);Zero")
 
karim said:
well, I tried this:

txt2.Text = CStr(CDbl(txt1.Text) / 2250)

but it's showing lots of decimals. how can i get it to show only 2
or 3 decimals?

and thanks partice for leting me see that i didn't need the
Debug.print or the Math.Round.

the code works, i just needed the decimals. and now that i have the
decimals, they are too much...


txt2.Text = (CDbl(txt1.Text) / 2250).ToString("0.000")


See also:
http://msdn.microsoft.com/en-us/library/7wchwf6k.aspx

In addition, I suggest to add some checks because the text in txt1.text
might not be convertable to a Double. CDbl will fail then. For example:

Dim d As Double

If Double.TryParse(txt1.Text, d) Then
d = d / 2250
txt2.Text = d.ToString("0.000")
Else
MsgBox("invalid input", MsgBoxStyle.Information)
End If



Armin
 
Back
Top