468,727 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

Decimals

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
Oct 20 '08 #1
8 3000
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

"Patrice" wrote:
Are you talking about what is shown in txt2 (is the Debug.print line
relevant ?)

What is the part of the code that makes you think it should behave this way
? (Math.Round ???)

--
Patrice

"karim" <ka***@discussions.microsoft.coma crit dans le message de groupe
de discussion : 13**********************************@microsoft.com...
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

Oct 20 '08 #2
"karim" <ka***@discussions.microsoft.comschrieb
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

Oct 20 '08 #3
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 #.###

Oct 20 '08 #4
No the format$ *function* takes two input values and return a value. Then
you print this returned formatted value... Period.

See also Armin post that contains basically my second round of remarks but I
wanted first to clarify why you thought you should have seen that before
moving on more advanced topic.
You may want to read again a programming introduction tutorial before moving
on.

--
Patrice

"karim" <ka***@discussions.microsoft.coma écrit dans le message de groupe
de discussion : 45**********************************@microsoft.com...
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

"Patrice" wrote:
>Are you talking about what is shown in txt2 (is the Debug.print line
relevant ?)

What is the part of the code that makes you think it should behave this
way
? (Math.Round ???)

--
Patrice

"karim" <ka***@discussions.microsoft.coma crit dans le message de
groupe
de discussion : 13**********************************@microsoft.com...
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

Oct 20 '08 #5
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...
Oct 20 '08 #6
karim wrote:
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
Oct 20 '08 #7
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" <ka***@discussions.microsoft.comwrote in message
news:EE**********************************@microsof t.com...
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...

Oct 20 '08 #8
"karim" <ka***@discussions.microsoft.comschrieb
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

Oct 20 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Oswald | last post: by
reply views Thread by thx | last post: by
2 posts views Thread by westjon64 | last post: by
6 posts views Thread by Donal McWeeney | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
xarzu
1 post views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.