vb.net currency display has four zeros instead of two - vb.net

when i get the money field from sql server to vb.net code, i always get 1.0000 instead of 1.00. how do i convert this to 1.00 in vb.net?
TD = New HtmlTableCell
If Not SqlDR("Price") Is DBNull.Value Then
TD.InnerHtml = SqlDR("Price")
Else
TD.InnerHtml = "0.00"
End If
SQLDR is my sql data reader

That is because SQL Server stores the MONEY field with 4 decimal places. To see it with 2, use the String.Format method.
String.Format("{0:c}", 10) ''result: $10.00
String.Format("{0:N2}", 10) ''result: 10.00
See these pages for more ways to format your numbers
Standard Numeric Format Strings
Custom Numeric Format Strings

You need to format the data when you output it:
myMoney.ToString("0.00");

Are you sure you aren't confusing the display of the value with the actual value?
1.0000 and 1.00 are the same value.
If you want to display only a certain number of places when converting a value to a string, you should look at the Custom Numeric Format Strings section in the MSDN documentation to figure out the format string to pass to the ToString method on the Decimal, Double, Single structures, or the static Format method on the String class.

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

VBA Currency to Text Conversion

I have a list of currency values in one WorkSheet such as $4,250.00 that I want to compare to the equivalent text/string; e.g. "$4,250" in another Worksheet. When I convert the currency value to text prior to the InStr comparison, I lose the "$" and "," resulting in "4250" which makes the comparison false. Is there a way to convert the currency value to text and not lose the format?
Thanks
A numeric value can be formatted to currency in VBA using Format(value, "Currency"), e.g. Format(Range("A1").Value, "Currency").
Also, the .Text property will return the formatted string, so Range("A1").Text would give you a string of $4,250.00 if cell A1 contained 4250 and was formatted in a standard currency format.
In Excel itself, I would suggest using a formula such as =TEXT(A1,"$#,##0.00").

Concatenate string and number as number

I have a text made of some cells with concatenation. One of the items of the text is a number. How to make it a full number with all the necesarry comas etc.
I have: on stt 03/06 db corr PLN 60000000 val 03/06 pending
I need: on stt 03/06 db corr PLN 60,000,000.00 val 03/06 pending
I tried with CDbl(Sheets(1).Cells(i, 5).Value) but the number is still the same.
Please note that the source cell contains the number in correct format : 60,000,000.00 which is downloaded from DB2 database
Many thanks for any help how to achieve that
To concatenate a string and a number you just use &, then convert the result to a number with Val() function.
Then if you want to format the result number you can use Format() function. Then you get a formatted string.
Example:
j = Val("10" & 10)
s = Format(j, "##,###.00")
I think that the use Format Function, resolve your problem -> https://msdn.microsoft.com/en-us/library/office/gg251755.aspx
Maybe format(60000000,"##,##0.00")?
If your cell is already formatted appropriately, you can use its Text property:
Sheets(1).Cells(i, 5).Text
Note that because this is the displayed value, you can get #### returned if the column width is too narrow for the actual value, but it's still useful where you don't necessarily know the format ahead of time.

Populate DataGridView Column With a Double forcing to 2 Decimal Places, with Sort

Sorry for the weird title but I didn't know how to phrase it any better.
I'm trying to populate a column on my DataGridView with a number that has 2 decimal places.
To do this I'm using
Dim _Size as Double = 1.2345
DataGridView1.Item(0, 0).Value = Format(_Size, "0.00")
This populates the data correctly, but when sorting by this column, it treats the item as a string.
I found on the net, that if you convert the data being entered to a number type (double, integer etc..), it will then sort it like a number instead of a string.
This works brilliantly, but any values that are integers with 2 decimal places (i.e. 1.00) are changed to 0 decimal places.
So, if I had the following values
1.2345
2.2345
3.2345
4.2345
5.0011
and I formatted as 2 decimal places they would then become
1.23
2.23
3.23
4.23
5.00
if I then converted them back to doubles they then become
1.23
2.23
3.23
4.23
5
Is there any way of populating a DataGridView with these values formatted as 2 decimal places but keeping the double type so that the column sorts correctly?
I hope I've explained this clearly.
Any help would be greatly appreciated
You are converting your values to string by using the legacy VB Format function:
Function Format(Expression As Object, Optional Style As String = "") As String
When it is converted, the other decimals are lost. Use the Format Property of the DefaultCellStyle for that column to specify the number of decimal places you want (N2 for 2 decimals). Unlike VB's Format, this acts like a "DisplayAs" without altering the value or changing the Type.
In the Properties pane, select Columns to start the column editor
Pick the column
Click the DefaultCellStyle property
For Format, click the ... button to select from a list. Numeric and 2 Decimals results in N2
If you store a numeric values in that column, only 2 decimal places will display, but the actual/original value will still be available and sorting will using the numeric value rather than a text sort.

Visual Basic format number to hower many number on left of decimal and 1 decimal without rounding

I am acutally using SSRS but it is for an expression so this is VB code. I am wondering how I would get a number such as 236.4723423 to appear at 236.4 instead of 236.5, so basically I jsut want to truncate it always after 1 decimal.
I tried Format = "N1" this rounds it
I tried Formate = "#######.0" and "######.#" and this rounds it as well.
Any ideas?
value = Math.Floor(value * 10) / 10
Use the format "########.00", then once it's in string form, trim the last char.
Edit:
Dim myString as String
myString = CStr(FORMAT(((SUM(Fields!Shipment_Weight.Value)) / 2000),"######.00"))
myString = myString.Substring(0, myString.Length - 1) & "T"
You will probably want to implement a custom IFormatProvider interface to do this. There is a great example on MSDN here.