I'm trying to convert double to string in format of exponential notation.
for example:
double l_dNum = 3.333;
string l_strNum = l_dNum.ToString();
Console.WriteLine(l_strNum);
//Wanted results: 3.33e+00
Thanks!
From MSDN examples
string l_strNum = l_dNum.ToString("E");
If you want up to 2 decimals only
string l_strNum = l_dNum.ToString("E2");
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;
}
Is it possible to perform string formatting using c#'s string interpolation with a specific CultureInfo?
For example if you are in a en-US System and want the number to be formatted with pt-PT how to I achieve that with string interpolation?
You can use CultureInfo.CreateSpecificCulture for specifying the culture and the C formatter for converting the value into a string with a currency.
double amount = 50.5;
string str = $"{amount.ToString("C", CultureInfo.CreateSpecificCulture("pt-PT"))}";
Interpolated strings can be converted to FormattableStrings, which can be formatted with a IFormatProvider:
var formattableString = (FormattableString)$"{1.1:C}";
var result = formattableString.ToString(new CultureInfo("pt-PT"));
// "1,10 €"
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 need help on C# string formatting an integer to a decimal. I cannot find a way to do this.
How can I convert an integer number like "45099" to a decimal like "450.99"?
Txs in adv
Ariel
decimal d = (decimal)45099 / 100;
This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 9 years ago.
I need to set “0” at the end of the decimal place dynamically if less integer number found after decimal place.
Suppose we have value: “535.8”
Now I need to set it as “535.800”
I have following code:
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
Console.WriteLine(price);
Console.ReadLine();
Unable to get 535.800.
How can I achieve this?
You can convert price to string and show upto 3 decimal places with 0's at end.
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
//string.Format("{0:N2}", price);
Console.WriteLine(string.Format("{0:N3}", price));
price.ToString("N3")
Standard Numeric Format Strings: The Numeric ("N") Format Specifier
So if the number of decimal should be dynamic:
int numDecimalPlaces = 3;
Console.WriteLine(price.ToString("N" + numDecimalPlaces));
You can use string.Format() to make it possible:
Console.WriteLine(string.Format("{0:N3}", d));
So in your code:
string cost = "535.8";
string decplace = "3";
decimal price = decimal.Round(Convert.ToDecimal(cost), Convert.ToInt32(decplace));
Console.WriteLine(string.Format("{0:N" + decplace + "}", price);
Console.ReadLine();