Hello,

I've got a question about some mathematics in VB.net.
I really hope someone can help me out!

On a form i've got two textboxes.
Users can give input (numbers) and the numbers are like this: 0.000 or 250.000 or 36547.256, etc.
The numbers are converted to a string for the output, but..

In some case, a value of 50 must be deducted from the value, so it will be like this,
250.000 - 50.000 = 200.000 or 36547.256 - 50.000 = 36597.256
The program is doeing like this:
250.000 - 50.000 = 249950.

Can somebody help me with this?

Thanx

show your code..

When doing the maths part convert the input text to type double:

Sub Addition()
dim Answer as Double

If IsNumeric(input1.text) AndAlso Isnumeric(input2.text) then
    Answer = cDbl(input1.text) + cDbl(input2.Text)
    Output.Text = Answer
Else
    Output.Text ="ERROR NON NUMERIC ENTRY"
END IF
End Sub

Or else when faced with "250" + "7.5" the code may convert the "250" to an Integer and the "7.5" to a double with the erratic results you are getting...

"Always add apples to apples and oranges to oranges or else you get fruit salad."

Ok, here is the code

Inputstart can be: 5.000 or 100.000 or. 254.654, etc.
InputEnd can be: 250.000 or 4568.354 or 5000.000, etc.

Public InputStart As String
Public InputEnd As String
Public Dkstr As Decimal
Public DS As Decimal = 50.0

InputStart = Start
InputEnd = End

If InputEnd - InputStart < 50.0 Then
Dkstr = InputStart
Else
Dkstr = InputEnd - DS

End If

Thanx

250.000 - 50.000 = 249950.

Does your system by any chance use a locale where . is used as a thousands separator and , as the decimal point? Because if so, the users should either input the numbers in a format compatible with the current locale (i.e. use , instead of .) or you should explicitly use a different locale when converting the string to a number.

Thank you all for the help.

The problem is solved, but I've got a new question.

When the result is 300, the value is displayed as 300.000 (this is how I want it to be), but.
When the result is 3000, the value is displayed as 3.000.000. I would like to have the result like 3000.000, without the thousand separator.

Can anyone help me with it?

Thanx!

Again, use a locale that formats the numbers in the way you want them to be formatted.

Also: It shows the result as "3.000.000", not "3.000,000"? How did you get that to happen? You didn't just multiply the result by 1000, did you?

The result is 3,000.000. I didn't multiply it bij 1000.
Is there something possible with format string?

Do I first have to change it to a string with convert.toString and than format string? So yes, what is the format type?

Can you be more specific in your awnser?

See my post earlier, you should convert the values to doubles to ensure you are adding like with like.

Alright,

I've tried to solve the problem, but it won't work. Here is my code:

Start = 20.000
End = 300.000 (or 3000.000 or 30000.000)

Public InputStart As Double
Public InputEnd As Double
Public Dkstr As Double
Public DS As Double = "50.000"

Public Function dbldkstr(ByRef InputStart As Double, ByRef InputEnd As Double, ByVal Dkstr As Double, ByRef Start As String, ByRef Eind As String, ByVal DS As Double)

InputStart = Start
InputEnd = End

Convert.ToDouble(InvoerStart)
Convert.ToDouble(InvoerEind)

If InvoerEind - InvoerStart < DS Then
    Dkstr = InvoerStart
Else
    Dkstr = InvoerEind - DS
End If

Dkstr = Convert.ToString(Dkstr)
dbldkstr = Dkstr
dbldkstr = FormatNumber(Dkstr, TriState.False, TriState.True)

First I converted the strings to double, then the if statement is running, then i want the Dkstr to convert to a string and have it a format like 250.000.
This works until the user gives a value of 3000.000 or 30000.000. Then, the annoying separator is there and gives a result like 2.950.000 instead of 2950.000.

I really hope that you guys can help me with this now i have given you the code.

Thanx

According to MSDN the last argument to FormatNumber tells it whether or not to use a thousands separator. If you leave out the argument (which you did), it uses the current locale's default. So if you don't want to change the current locale, you should pass TriState.False as the last argument.

Edit: Shouldn't the second argument to FormatNumber be an integer?

I've put the TriState.False as last argument, but it doesn't work.
When I do that, the result is 29500000 and not 2950.000.

Okay, I've looked on your code more closely. On line 7 you've assigned a string to a double. This will convert the string to a double using your current locale. So on your system this causes the value of DS to become fifty thousand, not 50.

If you want to disregard locale settings, you should not be doing any implicit conversions from string to double or vice-versa. Always use explicit conversions, in which you explicitly specify how you want to format the numbers. If that's too annoying, you might want to consider changing the locale for your program (which I might have suggested before in this thread).

If you remove the quotes from line 7 and remove lines 23 and 24, your code should work (at least with regards to how the result of dbldkstring is formatted - InvoerStart and InvoerEind will still be converted using your current locale).

OK guys,

I've solved the problem.
After the calculation I converted to a String.
Then, I used formatnumber to get the result in the right format.

Thanx guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.