473,407 Members | 2,314 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,407 software developers and data experts.

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 3257
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
0
by: thx | last post by:
I have MySQL 4.0.18 on Windows 2000 and I have a problem with floats and decimals. The table has floats and decimals fields but when I enter non-integers in these fields, they get rounded. So if I...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.