Click here to Skip to main content
15,065,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I do some calculations by using values from labels in which those labels get values from SQL.
If I use a simple way of doing division then I get very big value,
For example :

LabelTotal.Text = LabelTotal.Text / textDivide.Text

and I do division between 300/3, 300 being the number that comes from SQL, and 3 is the number that I insert manually in my and I get 5 digits results without Comma or full stop separation.

What I have tried:

I tried this version of code and it did not work,
'Dim val1 As Int32 = Convert.ToInt32(Labalsum.Text)
        'Dim val2 As Int32 = Convert.ToInt32(textDivide.Text)
        'Dim val3 As Int32 = val1 / val2
        'LabelTotal.Text = Convert.ToString(val3) 

on val1 it gives me this error :
Input string was not in a correct format.

And I tried other way which is this :

Dim res As Integer = 0
res = Convert.ToInt32(LabelTotal.Text) / Convert.ToInt32(textDivide.Text)
Labalsum.Text = res.ToString()

and it gives me the same error. I would like to have the result with a full stop when it reaches 1K. Example: "1.500,25 "
Any Help is much appreciated.
Updated 2-Aug-20 4:00am
CHill60 2-Aug-20 9:40am
The text in your labels is not entirely numeric which is why you get the errors. Use int.TryParse instead of Convert.ToInt32.
For formatting your results look at the parameters you can use with .ToString
Member 13410460 2-Aug-20 12:24pm
Thank you for your reply, it won't allow me to use tryparse.
CHill60 2-Aug-20 19:29pm
What won't allow you to use .TryParse? You tagged your question VB.NET!

What is the string value: Labalsum.Text ?

Seems it is not a valid integer value that can be converted using Convert.Int32 and thus the error.
It may have a decimal point or alphabetic or any punctuation characters. Make sure to have a valid string for conversion.

If you cannot handle the string value, you can use: Int32.TryParse Method (System) | Microsoft Docs[^]. With it's usage you can first see if the conversion was successful or not and accordingly any code based on it.
Member 13410460 2-Aug-20 12:25pm
Hello, Thank you for your reply, it has EURO sign in front of the number.
Sandeep Mewara 2-Aug-20 12:53pm
You need to remove it so that it's just the numbers and then your conversion would work out. :thumbsup:
Member 13410460 2-Aug-20 16:20pm
Thank you for your help, Is there a way to do it with euro sign ? its kind of money-related. Thank you
Don't assume that the text that is input will be valid. You need to check all inputs firs. So use Int32.TryParse as explained at Int32.TryParse Method (System) | Microsoft Docs[^]. Note, if the numbers are not integers then use Double rather than Int32.
Member 13410460 3-Aug-20 11:09am
Numbers are integer, just that in labelsum.the text comes value with EURO Sign in it.
Richard MacCutchan 3-Aug-20 11:17am
The euro sign is not a numeric digit, so you first need to parse that out.
Member 13410460 3-Aug-20 11:18am
Thank you for the reply, How can I parse only the EURO Sign out of it?
Member 13410460 3-Aug-20 11:19am
I mean when I use it this way: Labels.Text = LabelTotal.Text / textDivide.Text it works just the comma is not in the proper place and there is no error.
Richard MacCutchan 3-Aug-20 11:48am
Split the string, or use the Replace method.
Richard MacCutchan 3-Aug-20 11:51am
That is not possible since there is no / operator for strings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900