Substraction of integer values in string variables in asp.net - asp.net

I am developing a website in asp.net and have this problem:
string price = row["Price"].ToString();
string discount = row["Discount"].ToString();
This code is for taking data from a DataTable and it is working correctly. Both are floating point values(12,50.75...). In my program I want to subtract "discount" from "price" and assign the result to a new string. Suppose string price contains 50 and string discount contains 23.5, then I want 26.5 in the new string. How do I accomplish this?

You can you the below code:
string strPrice = row["Price"].ToString();
string strDiscount =row["Discount"].ToString();
string strFinal = (Convert.ToDouble(strPrice) - Convert.ToDouble(strDiscount)).ToString();

string newPrice = (double.Parse(price)-double.Parse(discount)).ToString();

string strDiff = (Convert.ToDouble(row["Price"].ToString()) - Convert.ToDouble(row["Discount"].ToString())).ToString();

you can convert double string variable to double using Convert.ToDouble or using double.Parse() function.
for difference between these two check
double price ,discount,amount;
string result = null;
price = double.Parse(row["Price"].ToString());
discount = double.Parserow["Discount"].ToString());
amount = price - discount;
string result = Convert.ToString(amount);

yes you can do like this way
double amount=(convert.toDouble(row["price"].tostring()))-(convert.toDouble(row["Discount"].tostring()));
string Totalamount=amount.tostring();

try this..
double amount=(convert.todouble(row["price"].tostring()))-(convert.todouble(row["Discount"].tostring()));

You need double.Parse to make them number and perform arithmetic operations on them.
double price = double.Parse(row["Price"].ToString());
double discount = double.Parse(row["Discount"].ToString());
double amount = price - discount;

Related

Convert 15 digit number string to java8 timestamp

I am struggling to convert a big 15 digit number string to a date format.
Sample code :
String dateInString = "201410051252323";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ");
LocalDateTime dateTime = LocalDateTime.parse(dateInString, formatter);
I am getting following exception
java.time.format.DateTimeParseException: Text '2014100591523' could not be parsed at index 0
Can anyone please suggest the best way to do this in Java 8
You have the right the idea, but there are two issues with your code--one syntactic, and the other logistic:
Your pattern expects the string to be formatted with hyphens and colons and spaces. For example, "yyyy-MM-dd" will match "2014-10-05", but not "20141005". To match the latter, just drop the hyphens and use "yyyyMMdd".
You use a 'Z' in your pattern for the final digit, indicating that you expect a time zone offset in your string. But the 'Z' pattern matches offsets like "+0000" and "-8000", not a single digit. Also, to represent datetimes with a time zone, you should use the ZonedDateTime or OffsetDateTime class instead of LocalDateTime
I'm not sure which time zone the '3' in your example string is supposed to represent, if it is indeed supposed to represent a zone offset at all. If you do not have control over the formatting of your data set, you'll have to peel off the final digit and handle it separately.
String dateInString = "201410051252323";
String dateTimeString = dateInString.substring(0, 14); // "20141005125232"
String zoneDigitString = dateInString.substring(14); // "3"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); // no "Z"
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
/* Manually convert zoneDigitString to a ZoneId here */
ZoneId zone = ...;
ZonedDateTime zonedDateTime = dateTime.atZone(zone);
You'll have to handle the "..." part on your own, depending on what "3" represents.

Checking content of textbox vb.net

I am using a text box for input to my SQL query. Based on the input I create a certain query and display the data in a gridview.
However I wish to make an adjustment for my users.
They often make an input like PL26... However this is not a valid name in the database to search for. Therefore I want to CHECK their input, and alter it accordingly, so they don't have to think about it.
I happen to know that when they type PL26 the correct input would be PL000026 ... The entity to search for is always "PL" + 6 characters/numbers... so if they wrote PL155, the number/string I pass to the sql query should become PL + 000 + 155 = PL000155.
I hope someone can help me how to accomplish this. That is if it is possible?
My idea/Pseudo code would be something like
If tbInput.txt's FIRST 2 CHARS are PL, then check total length of string
if StringLength < 8 characters, then
if length = 2 then ADD 4 0' after PL...
if length = 3 then add 3 0's after PL...
if length = 3 then add 3 0's after PL..
etc
....
...
Here we go:
Private Sub Button21_Click(sender As System.Object, e As System.EventArgs) Handles Button21.Click
Debug.Print(formatCode("PL1"))
Debug.Print(formatCode("PL"))
Debug.Print(formatCode("PL01"))
Debug.Print(formatCode("PL155"))
End Sub
Private Function formatCode(userInput As String) As String
Dim returnVal As String
If userInput.Length < 8 Then
returnVal = String.Concat(userInput.Substring(0, 2), userInput.Substring(2, userInput.Length - 2).PadLeft(6, "0"))
Else
returnVal = userInput
End If
Return returnVal
End Function
You may need to add some validation ensuring it starts with PL etc.
The following will work as long as there are no other non-numeric characters in between the PL and the numbers. You can always add it in your validation.
Dim newInput As String
If (input.StartsWith("PL")) Then
input = input.Remove(0, 2)
End If
' If this fails then it means the input was not valid
Dim numberPart = Convert.ToInt32(input)
newInput = "PL" + numberPart.ToString("D6")
Exctract a number by removing prefix "PL"
Parse to Integer
Use Custom Numeric Format Strings(zero placeholder) for adding zeros and prefix
Const PREFIX As String = "PL"
Dim exctractedNumber As Int32
If Int32.TryParse(tbInput.txt.Replace(PREFIX, String.Empty), exctractedNumber) = False Then
'Error nahdling
Exit Sub
End If
Dim finalFormat As String = String.Format("'{0}'000000", PREFIX)
Dim finalValue As String = exctractedNumber.ToString(finalFormat)
I would make use of the handy PadLeft method:
Dim input As String = "PL26"
Dim number As String = input.Substring(2, input.Length - 2)
If number.Length <> 6 Then
number = number.PadLeft(6, "0"C)
End If
MSDN String.PadLeft

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

I get the following error when trying to create a default row in the asp.net dropdown: Conversion from string "x" to type 'Integer' is not valid.
CmbSalesAgents is the drop down control.
DefaultSalesAgent is the entity object (has values)
x is a concatenation of a numeric value and a string
**
Dim DefaultSalesAgent = (From o In db.PayoutRegisters
Join s In db.SalesAgents On s.SalesAgentId Equals o.SalesAgentID
Where o.PayoutRegisterID = PayoutRegisterID
Select o.PayoutRegisterID, x = s.CSRName + " (" + o.PaidThruDate.ToString + ")").ToList
If DefaultSalesAgent.Count > 0 Then
CmbSalesAgents.Items.Insert(0, New ListItem(DefaultSalesAgent.Item("x").ToString, PayoutRegisterID))
Else
CmbSalesAgents.Items.Insert(0, New ListItem("Select Sales Agent Payout Register", 0))
End If
Since DefaultSalesAgent is a list object then you should access its items through integer index not a string:
DefaultSalesAgent.Item(integer_index)
If you specifically need to insert the row based off a string value, you can replace
DefaultSalesAgent.Item("x")
With
DefaultSalesAgent.Item(DefaultSalesAgent.FindIndex(x => x.StartsWith("x")));
You can also use contains instead of startswith depending on your needs. Please note this is only if you specifically need to look for strings as it's a lot more expensive than accessing an index in the list.

How to do a simple maths percetange calculation correctly in vb.net?

Can this equation be reworked to a correctly formatted string?
Dim Total = MyNumber / 100 * MyVAT
Produces error:
"Input string was not in a correct format"
try with this statement
Dim Total = Val(MyNumber) / 100 * Val(MyVAT)
You should really set Option Strict to on. Then this would never compile since MyNumber is a string(as you've commented).
You should first ensure that the input is numeric, for example with Decimal.TryParse:
Dim Total As Decimal
Dim num As Decimal
If Decimal.TryParse(MyNumber, num) Then
Total = num / 100 * MyVAT
End If
Now you can use Decimal.ToString(format) to convert it to a string with the desired format. For example(assuming you want two decimal places):
Dim output As String = Total.ToString("N2")
Standard Numeric Format Strings
use like below
Total.ToString("#")
More Detail:- http://msdn.microsoft.com/en-in/library/0c899ak8.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Count Number of days in asp.net

I have 2 fields fromDate and toDate for a school's term. Say their values are '01-06-2013' to '01-09-2013'. How do I get the total number of days in the term, in vb.net?
You can try this in vb.net:
Dim t1 As DateTime = Convert.ToDateTime("01-06-2013")
Dim t2 As DateTime = Convert.ToDateTime("01-09-2013")
MessageBox.Show(t2.Subtract(t1).Days)
I think сlass DateDiff can help you: http://msdn.microsoft.com/ru-ru/library/ms189794.aspx
Try this (C#):
TimeSpan timespan = Convert.ToDateTime("01-09-2013").Subtract(Convert.ToDateTime("01-06-2013"));
Response.Write(timespan.Days+1);
Usually the Subtract method return a value that is one less than the correct value so added one to get excat no of days
OR:
DateDiff(DateInterval.Day , curDate, srDate) //can replace the first argument with "d"
Also check this link

Resources