Concat decimal value with string - sql

I am retrieving data from the database and displaying in Gridview. I have some data in which I want to display % sign at the end (in gridview) what would be the best way to achieve this. Can I concat % sign in sql select statement or I have to add using vb code behind.

The best way is to use DataFormatString property from the Gridview columns and set it to something like "{0:0.00}%". This will format your results by using the number, 2 decimal positions and adding the % symbol to the end.
If you have rows with numeric values and rows that already contain % or other special characters, the best way to assure that the data is correctly visualized would be to remove the % or special characters before setting the Gridview data source. This way you will allways get the same result for each row.

I loop through the Gridview row and concat % sing at the end
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(6).Text = 4 Then
e.Row.Cells(5).Text += " %"
End If
End If

Related

Convert string to double in word (vba)

I am trying to convert a text in a word document to be a double, so I can do currency formatting on it. I receive this text from a mail merge. How would I create a macro that can receive this text and return it as a number?
I'm unfamiliar with word, and VBA script. What I have made so far is
Function stringToDouble(baseString As String)
Dim num As Double
num = Val(baseString)
stringToDouble = num
End Function
I'm not sure how I would call this macro. Because it takes a parameter it does not show up in the macro table.
I may be completely off on how to convert text to a double in word, but any help is appreciated.
Thanks. Please comment for any clarifications.
You don't need a macro for this!!! All you need do is learn how to use formatting switches in Word fields.
To control number & currency formatting in Word, add a numeric picture switch to the mergefield. To do this:
select the mergefield;
press Shift-F9 to reveal the field coding. It should look something like {MERGEFIELD MyData};
edit the field so that you get {MERGEFIELD MyData # $,0.00} (or whatever other numeric format you prefer - see below);
position the cursor anywhere in this field and press F9 to update it.
Note 1: The '# $,0.00' in the field is referred to as a numeric picture switch. Other possibilities include:
# 0 for rounded whole numbers
# ,0 for rounded whole numbers with a thousands separator
# ,0.00 for numbers accurate to two decimal places, with a thousands separator
# $,0 for rounded whole dollars with a thousands separator
# "$,0.00;($,0.00);'-'" for currency, with brackets around negative numbers and a hyphen for 0 values
Note 2: The precision of the displayed value is controlled by the '0.00'. You can use anything from '0' to '0.000000000000000'.
If you use a final ';' in the formatting switch with nothing following, (eg # "$,0.00;($,0.00);") zero values will be suppressed. Note that this suppresses 0s resulting from empty fields and from fields containing 0s.
Note 3: If you use a decimal tab or right-aligned tab to align the values, wrap the switch in quotes (i.e. # "$,0.00") and insert a tab into the field code after the $ sign, you can have the values output with the decimal alignment occurring after the $ sign.
For more Mailmerge Tips & Tricks, see: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
If you want to convert the number to a Double data type then try this:
Function StringToDouble(ByVal baseString As String) As Double
StringToDouble = VBA.CDbl(baseString)
End Function
If you're only concerned about formatting currency, convert the string to the Currency data type like this:
Function StringToCurrency(ByVal baseString As String) As Currency
StringToCurrency = VBA.CCur(baseString)
End Function
You will still need to format the number but both functions give you a number that can be formatted.
Here's an example that also gives you a string formatted as USD (e.g. $4,999.75). It requires the StringToCurrency function above.
Sub test()
Dim stringNum As String
stringNum = "4,999.754501"
Debug.Print "stringNum=" & stringNum ' outputs 4,999.754501
Dim currencyNum As Currency
currencyNum = StringToCurrency(stringNum) ' outputs 4999.7545
Debug.Print "currencyNum=" & currencyNum
Dim formattedString As String
formattedString = Format$(currencyNum, "$#,##0.00")
Debug.Print "formattedString=" & formattedString ' outputs $4,999.75
End Sub

VB.NET Datagrid / Datatable column decimal formating

I have got a data column representing integer values for example
value 100000, i would like to format display into 100.000 or 100 000. To fix this i used code below but seems it doesn't work in my case.
dt.Rows(i).Item("QORDPO") = FormatNumber(ilosc, 0,,, TriState.True)
If you are using a DataGridView (as the tags indicate) have you tried the DefaultCell Property on the column? There are various option one of which is Custom, if the ones presented don't suit.
Examples of setting the column format properties in code:
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "C" '--- currency
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "N1" '--- number
datagridview.Columns("aColumn").DefaultCellStyle.Format() = "d" '--- date

Textbox value summation

I have two textboxes in which users enter a number, Data1.text and Data2.text. I need to do a simple check before proceeding and closing a form, i.e. the process should check if the sum of the values in Data1 and Data2 textbox is greater or lesser than the value from a label text named FinalValue.text, which gets its value from my main form.
If Data1.Text + Data2.Text <> FinalValue.Text Then
MessageBox.Show("The sum of the entered values are different from the final value!")
Exit Sub
End If
The problem is that whatever the user enters in the two textboxes, the messagebox pops up.
For example...if the FinalValue label has a value of 58.50, and if the user enters in one textbox 50 and in the other one 8.50, the messagebox pops up. I suppose I have to format my textboxes or something like that, but not sure in which way.
Thanks,
When you do Data1.Text + Data2.Text, you are actually just concatenating the values, not doing a number Sum, you need to convert to decimal.
Something like :
If Decimal.Parse(Data1.Text) + Decimal.Parse(Data2.Text) <> Decimal.Parse(FinalValue.Text) Then
MessageBox.Show("The sum of the entered values are different from the final value!")
Exit Sub
End If
Assuming the values are integers , change your condition to match the integers sum, You are currently concatenating the strings
CInt(Data1.Text) + CInt(Data2.Text) <> CInt(FinalValue.Text)
Or for Decimals use Decimal.Parse() in place of CInt()

vb.net - Sort datagridview column numerically

I know this has been asked quite a few times, but i'm having issues with the solutions found on most other pages.
I have a single datagridview column that i want to be sorted by number (1,2,10 instead of 1,10,2)
Best i can see online, i need to convert the column or cell to an integer value type - but i'm not sure how to do so.
I've tried grid.columns(4).valuetype = typeof(System.int32), and tried the same for cells individually.
Trying above always results in a "int32 is a type in 'system' and cannot be used as an expression" error - which i'm not sure about.
The data itself is obtained froma text file, and converted from string to integer when being added into the cell datagrid_alltracks.Rows(shutupaboutlambda).Cells(4).Value = CInt(numstring))
You can just set the DataGridView SortCompare Event to compare two integers (or two singles, or two doubles). Code wise (calling your datagridview "grid")
Private Sub grid_SortCompare(sender as Object, e as DataGridViewSortCompareEventArgs) Handles grid.SortCompare
e.SortResult = CInt(e.cellvalue1).CompareTo(CInt(e.cellValue2))
e.Handled = True
End Sub
if you are doing single or double variables, use CSng or CDbl instead of CInt
e.SortResult = CSng(e.cellvalue1).Compareto(CSng(e.CellValue2)
You can do more fancy sorting if you want, You basically need to know that e.SortResult is Positive, Negative or Zero, and your cells are sorted according to that result (Positive keep order, negative reverse order, Zero - matched - do nothing (yet)). The current row index(s) and column are available in the e arguments so you can also compare adjacent column data if the current cells are matched)
If the grid is bound to a data source you could try
datatable.Columns.Add("ColumnName", GetType(Integer))
Else you may need to use the SortCompare event on the gridview.
See here
I know I'm coming to the party late, but I found that I once I had added the data, I needed to convert the desired columns to have a data type.
I'm adding data like the following:
DataGridView1.Rows.Add(New String() {CInt(recordnum), True, "play", wszName.ToString, qiOffset.ToString, value.ToString, qiLength.ToString})
Then, after all the data has been added, I then do a simple loop and convert the column, where I can then sort it. It's set up so you can do multiple columns if need be.
Dim colnum As Integer
colnum = 0 ' set this as your column to change the data type to
For i As Integer = 0 To DataGridView1.Rows.Count - 1
Dim d As Double = Double.Parse(DataGridView1.Rows(i).Cells(colnum).Value.ToString())
DataGridView1.Rows(i).Cells(colnum).Value = CInt(d)
DataGridView1.Rows(i).Cells(colnum).ValueType = GetType(Double)
Next
Sorting can work for whatever column you adjusted. In this case, it's column 4.
DataGridView1.Sort(DataGridView1.Columns(4), System.ComponentModel.ListSortDirection.Ascending)

How do I add two currencies together?

I am fairly new to programming. I am making a program where you enter a number into a textbox and when you leave the textbox, it converts your number to currency. I have also added another textbox which does the same.
This is what I want to happen:
If TextBox1.Text + TextBox2.Text =< 10,000 Then
Sum = 10,000
Else
Sum = TextBox1.Text + TextBox2.Text
However, (i think) because I am converting the numbers to currency before I add them together, it always returns 10,000 no matter what.
I know there is probably something pretty obvious I am missing. If you need any clarification, just ask! I appreciate your help.
+ concatenates strings, so TextBox1.Text + TextBox2.Text is a string and comparing it to the integer 10000 doesn't do what you want.
I would look for a text box control that lets you display numbers with a given format, but also gives you access to the numerical value as a property. Then you could add the values together without having to do a type conversion that may not work on your formatted currency string.
MaskedTextBox might give you some of that, but it's been a long time since I've used it.
You need to convert String values to Double (or any other numeric type), before adding them up.
Dim tbSum as Double = CDbl(TextBox1.Text) + CDbl(Textbox2.Text)
If tbSum =< 10000 Then Sum = 10000 Else Sum = tbSum