Unable to convert to double for cost number - asp.net

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).

Related

format string to specific format including dots and dashes

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"

How to extract the max value of a string in R

I have a vector of strings like this:
"1111111221111122111111UUUUUUUUUUUUUUUUUU"
"---1-1---1--111111"
"1111112111 1111" (with blank spaces)
everyone has different length and I want to extract the max value of the each string, for the three examples above the max values would be (2,1,2), but don't know how to do it with the letters or the dash or the blank spaces, all these three are the minimum, i.e., 1 is bigger than "U", "-" and " " and between them is the same.
Any advice?
Best regards
Decompose the problem into independent, solvable steps:
Transform the input into a suitable format
Find the maximum
The we get:
# Separate strings into individual characters
digits_str = strsplit(input, '')
# Convert to correct type
digits = lapply(digits_str, as.integer)
# Perform actual logic, on each input string in turn.
result = vapply(digits, max, integer(1L), na.rm = TRUE)
This uses the lapply and vapply functions which allow you to perform an operation (here first as.integer and then max) on all values in a vector/list.

Conversion from string "Price" to type 'Integer' is not valid

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)

convert string to double

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.

VBScript implicit conversion in IF statement different from variable to literals?

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.

Resources