I have string value in that I need to convert to double in VB.Net. Conditions are like below
string = "12345.00232232"
if condition is 3 (2 digits after decimal and comma)
display = 12,345.00
if condition is 5 (5 digits after decimal and comma)
display = 12,345.00232
If Condition is 7 ( 5 digits after decimal and no comma)
display = 12345.00232
How can I do that in VB.Net?
It sounds like you want to take a numeric input, convert it to double but then reformat it as a string based upon a numeric value for a specific style. Something probably like...
Public Function FormatNumericString(ByVal input As String, ByVal style As Integer) As String
Dim result As String = String.Empty
Dim temp As Double = Double.Parse(input) 'will throw on invalid input
Select Case style
Case 3
result = temp.ToString("#,##0.00")
Case 5
result = temp.ToString("#,##0.00000")
Case 7
result = temp.ToString("0.00000")
End Select
Return result
End Function
The basic thing is you have to convert the string to a double and use whatever formatting style you want. I've chosen to use double.Parse so that an exception would be thrown on an invalid input. double.TryParse could also be used, but it returns a true/false value rather than throwing an exception on an invalid input. It depends upon the behavior you want to follow.
Related
I have a string with numbers only and it always has the same witdh. I need this string in a specific format.
original = "00000000000000"
outcome = "00.000.000/0000-00"
Is there a way simple way to do this? Could it be applied to a vector of strings?
Assuming the width is constant, we can use sub:
original = "00000000000000"
sub("(.{2})(.{3})(.{3})(.{4})(.{2})", "\\1.\\2.\\3/\\4-\\5", original)
# [1] "00.000.000/0000-00"
if i print a variable containing an integer then why are brackets also printing in python 3.
Like this:-
count=5
a="The value of count is",count
print(a)
The output is like this:
('The value of count is', 5)
While I want the output to be like this:
The value of count is, 5
Actually I want to return the value of a so I have to do it like this only.
a="The value of count is", count
Creates a tuple and puts it into a. Printing the tuple will add the parenthesis and comma.
Just concatenate the count to the message and print that instead:
a = "The value of count is " + str(count)
Or you could write it inline using f-strings:
print(f"The value of count is {count}")
Which is similar to your use of format.
using vb asp.net
error: Problem with database (2). Conversion from string "Price" to type 'Integer' is not valid.
Dim testP As Decimal = reader3.GetDecimal("Price")
column price is decimal in database
The GetDecimal method accepts a column ordinal only, not a column name. The correct option would be to get the ordinal for that name:
Dim testP As Decimal = reader3.GetDecimal(reader3.GetOrdinal("Price"))
Alternatively, you could just get the Object reference from the Item property and cast it:
Dim testP As Decimal = CDec(reader3("Price"))
this works
Dim testP As Decimal = Decimal.Parse(reader3("Price").ToString())
convert int to decimal
Convert.ToDecimal(reader3("Price").ToString());
Try using the index of the column you have retrieved. Say your Select looked like this
Select Name, Department, Item, Price From MyTable;
Then Price would be index 3. (Zero based)
Dim testP As Decimal = reader3.GetDecimal(3)
I cant seem to get my cost value to show decimal places. The code below for value "100.00" only gives me "100" When I debug the value of Session("TotalProductCost") is 100D?
Session("TotalProductCost") = Convert.ToDecimal(Cost1) + Convert.ToDecimal(Cost2) + Convert.ToDecimal(Cost3) + Convert.ToDecimal(Cost4) + Convert.ToDecimal(Cost5)
Dim TotalProductAmt As Decimal
Decimal.TryParse(Session("TotalProductCost"), TotalProductAmt)
Dim Res As Decimal = TotalProductAmt
TotalAmount = TotalProductAmt.ToString()
TotalAmount = Res.ToString
If you want the result of ToString to always include two decimal places, you need to tell ToString that. You can pass a string parameter which tells ToString what kind of result you expect, like this:
TotalAmount = Res.ToString("F2")
The above tells ToString "I want the result to be fixed to two decimal places". If you hand it the value 1000, the string you get back will be "1000.00".
Alternatively, if you also want the digits to be grouped such that 1000 results in "1,000.00" (or some other grouping system, depending on which country you're in), you can pass in "N2" as your parameter to ToString:
TotalAmount = Res.ToString("N2")
For details of this and other string formats you can pass to ToString, see the MSDN documentation.
As for why Visual Studio is showing you "100D", the D signifies that you're looking at a decimal value (as opposed to an integer or whatever).
We are currently having an issue due to implicit conversion in an IF statement in VBScript (Classic ASP) that don't do implicit conversion the same way when dealing with a variable or a literal. Can someone explain this behavior to me, why do VBScript acts this way ?
Here is a sample of what I mean :
Const c_test = 3
Dim iId : iId = 3
Dim iTestStr : iTestStr = "3"
If iId = iTestStr Then
Response.Write("Long variable = String variable : Equal")
Else
Response.Write("Long variable = String variable : Not Equal")
End If
Response.Write("<br/>")
If c_test = iTestStr Then
Response.Write("Long constant = String variable : Equal")
Else
Response.Write("Long constant = String variable : Not Equal")
End If
Response.Write("<br/>")
If c_test = iId Then
Response.Write("Long constant = Long variable : Equal")
Else
Response.Write("Long constant = Long variable : Not Equal")
End If
Response.Write("<br/>")
If iId = "3" Then
Response.Write("Long variable = String literal : Equal")
Else
Response.Write("Long variable = String literal : Not Equal")
End If
Response.Write("<br/>")
If c_test = "3" Then
Response.Write("Long constant = String literal : Equal")
Else
Response.Write("Long constant = String literal : Not Equal")
End If
Which ouputs :
Long variable = String variable : Not Equal
Long constant = String variable : Not Equal
Long constant = Long variable : Equal
Long variable = String literal : Equal
Long constant = String literal : Equal
Which is quite confusing o_O
This is the result of one documented behavior and one undocumented one.
The documented behavior is that in comparisons, a number is always less than a string. This is mentioned in the documentation for Comparison Operators. Paraphrasing the table near the bottom of the page:
If one expression is numeric and the other is a string, then the numeric expression is less than the string expression.
The undocumented behavior is that comparisons involving literals are handled differently from comparisons involving variables. See this blog entry for more details. To summarize the important conclusion:
The relevant comparison rules in VB6/VBScript go like this:
Hard string ~ hard number: convert string to number, compare numbers
Hard string ~ soft number: convert number to string, compare strings
Soft string ~ hard number: convert string to number, compare numbers
Soft string ~ soft number: any string is greater than any number
The documented behavior explains why the first two comparisons are false, while the undocumented behavior explains why the last two comparisons are true.
You are (implicitly) declaring your variables As Variant so your If conditions actually test the equality of two Variants and determine that they are unequal.
In the last cases, however, you are using String constants (which can never be Variant, even if declared without a type) and String literals.
My guess is that when you compare two Variants, VB first determines whether they have the same type tag and if they don’t, resolves to False.