The string format data with mostly 4 decimal places needs to be converted into int.
I have tried this one but does not work and even directly using this Convert.ToInt16() but still did not worked:
Int32 result;
bool status = Int32.TryParse(v, out result);
Is there other ways to convert this?
Thanks.
You can convert it to Double first, and then convert to Int32
String s = "1.0000";
Double temp;
Boolean isOk = Double.TryParse(s, out temp);
Int32 value = isOk ? (Int32) temp : 0;
You can use the following:
string data = "1.0000";
int number
if(data.Contains('.'))
number = int.Parse(data.Substring(0, data.IndexOf('.'))); //Contains decimal separator
else
number = int.Parse(data); //Contains only numbers, no decimal separator.
Because 1.0000 has decimal places, first strip those from the string, and then parse the string to int.
You have to parse it as decimal and then cast to int.
decimal resultTmp;
bool status = Decimal.TryParse("1.000".Replace(".",","), out resultTmp);
int result = (int)resultTmp;
Also change '.' to ','
Related
I'm trying to convert a string to a decimal but have the last two values be the decimal point. Example: "001150" be converted into 11.50.
Everything I've found in my search will convert strings to a decimal but the string already has the decimal point. I might have to create a function that will make the decimal point out of the string but wanted to see if there are any other ideas first.
string input = "001150";
string convertedString = string.Format(
"{0:N2}",
double.Parse(input)/100.0
);
First check if string is a number, then divide by 100 and then format string:
public string ConvertTwoDecimals(string toConvert)
{
Double convertedValue;
var isNumber = Double.TryParse(toConvert, out convertedValue);
return isNumber ? $"{(convertedValue / 100):N2}" : $"{ 0:N2}";
}
if you need a decimal type without formatted:
public decimal ConvertToDecimal(string toConvert)
{
Decimal convertedValue;
var isNumber = Decimal.TryParse(toConvert, out convertedValue);
return isNumber ? convertedValue/100 : 0;
}
how to trim decimal part in given string of a decimal value in C#. im getting 20472.060 desired o/p - 20472
decimal totalamountWithTaxes = pri + result1 + result2 ;
string totalAmountPlusTaxes = totalamountWithTaxes.ToString();
You can simply do it like this:
int number = (int) totalAmountPlusTaxes;
or
string totalAmountPlusTaxes = String.Format("{0:C0}",totalamountWithTaxes);
Int32 Amount = Convert.ToInt32(totalamountWithTaxes.ToString().Substring(0,totalamountWithTaxes.ToString().IndexOf(".")));
Yes the above example is right and also I recommend you to visit:
String.Format Method-MSDN
I have a simple code in c# that converts a string into int
int i = Convert.ToInt32(aTestRecord.aMecProp);
aTestRecord.aMecProp is of string. The test I am running, during that it has values of 1.15 in string.
but above line throws error saying that input string wasn't in a format!
I don't understand why?
I am using VS 2008 c#
An integer can only represent strings without a decimal part.
1.15 contains a decimal part of 0.15.
You have to convert it into a float to keep the decimal part and correctly parse it:
float f = Convert.ToSingle(aTestRecord.aMecProp);
That is because 1.xx is not a integer valid value. You could truncate before converting to Int32, for sample:
int result = (int)(Math.Truncate(double.Parse(aTestRecord.aMecProp)* value) / 100);
if you are trying to validate that a string is an integer, use TryParse()
int i;
if (int.TryParse(aTestRecord.aMecProp, out i))
{
}
i will get assigned if TryParse() is successful
Try this:
double i = Convert.ToDouble(aTestRecord.aMecProp);
Or if you want the integer part:
int i = (int) Convert.Double(aTestRecord.aMecProp);
You can convert to double and then typecast it
string str = "1.15";
int val = (int)Convert.ToDouble(str);
Just try this,
Int32 result =0;
Int32.TryParse(aTestRecord.aMecProp, out result);
Do you need a C# equivalent for the JavaScript parseInt function? I have used this one on occasion:
public int? ParseInt(string value)
{
// Match any digits at the beginning of the string with an optional
// character for the sign value.
var match = Regex.Match(value, #"^-?\d+");
if(match.Success)
return Convert.ToInt32(match.Value);
else
return null; // Because C# does not have NaN
}
...
var int1 = ParseInt("1.15"); // returns 1
var int2 = ParseInt("123abc456"); // returns 123
var int3 = ParseInt("abc"); // returns null
var int4 = ParseInt("123"); // returns 123
var int5 = ParseInt("-1.15"); // returns -1
var int6 = ParseInt("abc123"); // returns null
ok I think this is
float d = Convert.ToSingle(aTestRecord.aMecProp);
I have an string AssetNumber that have the following format C100200.
so i need to do the folloiwng:-
Get all the characters after the first (e.g. 100200 using above example)
Convert the substring to integer
Return the integer + 1
but I do not know how to use the sub-string to get all the characters after the first char, and how to convert string into int? Any help on this will be appreciated.
var result = Int32.Parse("C100200".Substring(1)) + 1;
If you would like to have default value, if you can't parse current string:
int result;
if (!Int32.TryParse("sdfsdf".Substring(1), out result)) {
result = 42;
}
result+=1;
this is my code;
string a="11.4";
int b,c;
b=2;
c= convert.toint32(a) * b
I get this error;
Input string was not in a correct format
how can i convert "a"?
Well a is just not an integer value - you could use Convert.ToDouble() instead. To guard against parsing errors in case that is a possibility use double.TryParse() instead:
string a = "11.4";
double d;
if (double.TryParse(a, out d))
{
//d now contains the double value
}
Edit:
Taking the comments into account, of course it is always best to specify the culture settings. Here an example using culture-indepenent settings with double.TryParse() which would result in 11.4 as result:
if (double.TryParse(a, NumberStyles.Number, CultureInfo.InvariantCulture, out d))
{
//d now contains the double value
}
A very first glance, the number literal "11.4" is not a actual "int". Try some other converting format, such as ToDouble()
I have tried following code in C# for your reference.
string a = "11.4";
double num_a = Convert.ToDouble(a);
int b = 2;
double ans = num_a * b;