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
Related
Is it possible to dislay only the cents in an amount using only ToString(), something like 1.99.ToString("SOME FORMAT HERE")?
e.g. what if I want 1.99 to be displayed as "1 dollar(s) 99 cents"
($"{ Convert.ToInt32(amount) } dollar(s) { amount.ToString("???") } cents")?
This would work if amount is a double. If amount is string then you wouldn't need ToString() in following line. Note this is assuming a format you listed in your question:
string line = string.Concat("$", amount.ToString().Split('.')[0], " dollar(s) ", amount.ToString().Split('.')[1].Substring(0,2), " cents");
I don't think you will be able to do this with only the ToString method, so here's another way.
You can first get the decimal part by doing this:
var decimalPart = yourDouble - (int)yourDouble;
Then, you can times the decimal part by 100 and convert the result to an int.
var twoDigits = (int)(decimalPart * 100);
To convert this to a two digit string, you just pad 0s to the left:
var result = twoDigits.ToString().PadLeft(2, '0');
I can not do this using only ToString(), but this is almost what you want :)
double d = 15.12;
var str = d.ToString("0 dollar(s) .00 cents").Replace(".", string.Empty);
Anyway, I suggest you to create an extension method to do this:
public static class Extensions
{
public static string ToFormattedString(this double number)
{
var integerPart = Convert.ToInt32(number);
var decimalPart = Convert.ToInt32((number - integerPart) * 100);
return String.Format("{0} dollar(s) {1} cents", integerPart, decimalPart);
}
}
usage:
Console.WriteLine(d.ToFormattedString());
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 ','
I have formatted string, for example 123 456 000$ and I know format of this string {0:### ### ##0}$. I'd like to get int value = 123456000; from this string in C# with using known format.
How I can do that?
mytext = System.Text.RegularExpressions.Regex.Replace(mytext.Name, "[^.0-9]", "");
or something approximately like that get's rid of the pesky non-numbers
and then delete the first few characters 1,2,3... and last few length, length -1, length -3...
unless I'm missing something?
Oh yeah, and Convert.Toint32(mytext)
int.Parse("123 456 000$", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx
I think you will have to construct a similar NumberFormatInfo to parse the string value.$ for the currency symbol used in the text and in your case the thousand group seperator is space instead of , so a custom number format info should help you parse it.
string val = "123 456 000$";
NumberFormatInfo numinf = new NumberFormatInfo();
numinf.CurrencySymbol = "$";
numinf.CurrencyGroupSeparator = " ";
numinf.NumberGroupSeparator = " "; // you have space instead of comma as the seperator
int requiredval = int.Parse(val,NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands, numinf);
This should help you get the value
I would make it easy way:
String formattedString = "12345500$";
formattedString.Remove(formattedString.Length - 1);
int value = int.Parse(formattedString);
Please help me to write Regular expression on C# for getting Int64 value from string:
"NumberLong("634461051992965873")"
my string includes NumberLong part;
so as result must be 634461051992965873
Thank you!)))
string Temp = "Hax00r L33t";
string Output = Regex.Replace(Temp, "[^0-9]", "");
long num = long.Parse(Output);
long.Parse("634461051992965873")
does the job, but you could check long.TryParse too.
String txt = "634461051992965873";
int nbr;
if(Int64.TryParse(txt, out nbr)) {
// text can be converted to Integer
}
Can I use numbers while using String data type?
Sure you can, and if you want to use them as numbers you can parse the string. E.g. for an integer:
string numberAsString = "42";
int numberFromString;
if (int.TryParse(numberAsString, out numberFromString))
{
// number successfully parsed from string
}
TryParse will return a bool telling if the parsing were successful. You can also parse directly if you know the string contains a number - using Parse. This will throw if the string can't be parsed.
int number = int.Parse("42");
You can have numbers in a string.
string s = "123";
..but + will concatenate strings:
string s = "123";
string other = "4";
Debug.Assert(s + other != "127");
Debug.Assert(s + other == "1234");
Numbers can be easily represented in a string:
string str = "10";
string str = "01";
string str = 9.ToString();
However, these are strings and cannot be used as numbers directly, you can't use arithmetic operations on them and expect it to work:
"10" + "10"; // Becomes "1010"
"10" / "10"; // Will not compile
You can easily store numbers as a string:
string foo = "123";
but that only helps you if you actually want numbers in a string. For arithmetic purposes, use a number. If you need to display that later, us a format string.
String number1 = "123456";
keep in mind. using that number for arithmatic purpose, you have to convert that string into proper type like
int number1Converted = Int32.Parse(number1);
int.TryParse(number1 , out number1Converted );
for double
double doubleResult = 0.0;
double.TryParse("123.00", out doubleResult);