This question already has answers here:
How remove decimal part from string?
(3 answers)
Closed 1 year ago.
I want to remove decimal point from string.
I try with this code but not work for me:
string wSpeed = Math.Round(weatherBindingData.WeatherDataCurrent.Wind.Speed) * 3.6 + "km/h";
WindSpeed.Text = wSpeed;
I want to remove the decimal point, but with this code I receive a values with decimal point.
How is the correct way to remove the decimal point?
Use formatting:
string wSpeed = $"{Math.Round(weatherBindingData.WeatherDataCurrent.Wind.Speed) * 3.6:0}km/h";
Or what you more likely want:
string wSpeed = $"{weatherBindingData.WeatherDataCurrent.Wind.Speed * 3.6:0}km/h";
Related
This question already has answers here:
String to decimal conversion: dot separation instead of comma
(8 answers)
Closed 6 months ago.
I need a program that takes two inputs from the user and perform a calculation. Those inputs need to be double datatype.
The problem I'm facing is that: the program receives a decimal value with dot separator, but when converting it to double, it loses the decimal value. For example, input from the user is 2.5, but when converting it becomes 25.
When the user type 2,5 it is correctly converting it to 2.5
Here's my code example:
Console.WriteLine("Var1: ");
string? v1 = Console.ReadLine();
Console.WriteLine("Var2: ");
string? v2 = Console.ReadLine();
double v1Double = Double.Parse(v1);
double v2Double = Double.Parse(v2);
Console.WriteLine($"Var1: {v1Double}");
Console.WriteLine($"Var2: {v2Double}");
Console.WriteLine($"Multiplication: {v1Double * v2Double}");
Here's what I'm getting when with dot separator:
Here's what I'm gettig when comma separator:
Can anyone help me how to address this problem?
You could use NumberFormatInfo for this problem like so:
NumberFormatInfo provider = new NumberFormatInfo();
provider.NumberDecimalSeparator = ".";
provider.NumberGroupSeparator = ",";
double doubleVal = Convert.ToDouble(YOUR_STRING, provider);
This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 5 years ago.
I have a string array which contains 4 elements. Which looks like this.
How ever, when trying to do this:
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[2]));
I get the following error:
'Input string was not in a correct format.'
And that is because it's because the value uses a '.' but if I manually change the array to use a ',' it works.
How can I easiest replace all '.' with ','.
Use
//(float)Convert.ToDouble(words[1]),
(float)Convert.ToDouble(words[1], CultureInfo.InvariantCulture),
Try this...
Vector newVector = new Vector(
(float)Convert.ToDouble(words[1], CultureInfo.GetCultureInfo("en-US").NumberFormat),
(float)Convert.ToDouble(words[2], CultureInfo.GetCultureInfo("en-US").NumberFormat));
This question already has answers here:
How to convert this scientific notation to decimal?
(6 answers)
Closed 8 years ago.
In my application I get a number 2.424242E+08 in my label called lbl_price and when i try to convert it to a decimal i get a FormatEception. How can i fix this?
decimal newVat = Convert.ToDecimal(lbl_price.text);
Try:
decimal newVat = decimal.Parse(lbl_price.text, System.Globalization.NumberStyles.Float);
This question already has answers here:
How do I format a double to a string and only show decimal digits when necessary?
(2 answers)
Closed 9 years ago.
How to hide fractional part of a double or float number if it is all zero. I am converting a floating point number to string and its display Mantissa part even if it is zero. For example:
double number = 123.00;
string strNumber = number.ToString(); // it shows "123.0", what I need is only "123"
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString(); // it shows "123.2234" as needed.
Is there any built-in solution in .NET to get it done?
Thanks
Try using the overload of double.ToString() that takes in a format string, and pass it "R":
double number = 123.00;
string strNumber = number.ToString("R");
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");
This question already has answers here:
Truncate Decimal number not Round Off [duplicate]
(10 answers)
Closed 9 years ago.
I have a decimal number :
decimal a = 0.8537056986486486486486486486;
I want to show it as string with only 8 digit after point :
a equals -> "0.85370569".
How can I do it ?
For example, like this:
a.ToString("0.00000000");
Try using
a.ToString("D8");
This should convert it to the correct format.
Edit:
As said in the comments, D is only used for integral types, thus not for decimals and such.
Use
static void Main(string[] args)
{
decimal d = (decimal)0.8537056986486486486486486486;
Console.WriteLine(d.ToString("N8"));
Console.ReadLine();
}
instead.
This will format it to 0.85370570
To perform this numerically you could use:
decimal a = (decimal) 0.8537056986486486486486486486;
String test = (Math.Truncate(100000000 * a) / 100000000).ToString();
decimal a = 0.8537056986486486486486486486;
var v= a.ToString("#.########");