How to convert string to decimal with specific number of digits [duplicate] - c#

This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,

You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")

Related

Remove decimals without rounding [duplicate]

This question already has answers here:
How to remove decimal part from a number in C#
(8 answers)
Closed 2 years ago.
I have some variable length decimal numbers for ex:
1.123,
1.1234,
12.12345,
I need only the first 4 decimals. I can't use Math.Round() because I do not want rounded numbers, I just want to trim it and keep only first 4, like 1.1234, or 22.1234. Is there a way to accomplish this?
Thank you.
Multiply by 10000, store as integer to get rid off any remaining decimals. Divide by 10000.
var result = number.ToString("n4");
check Standard numeric format strings

Math Round in VS C# [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 5 years ago.
so I have the following problem I'm trying to output 2 numbers after the decimal comma but it doesn't output it with 2 numbers only when I have zero or more than one zero at the end of the number.
Console.WriteLine(Math.Round(s * 0.07, 2));
here's how I did it.
You might want:
Console.WriteLine($"{s:N2}");

C# double/decimal always have two decimals [duplicate]

This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
I need 45.7 to be 45.70
Math.Round(d, 2) have no effect.
Have tried with decimal and double type.
I would appreciate any kind of help.
You need to apply a string format:
string.Format("{0:N2}", 45.7)
Check here for docs on the string formatters

C#: Rounding up to 2 maximum decimal points for Double variables [duplicate]

This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Rounding double values in C#
(4 answers)
Rounding to 2 decimal points [duplicate]
(3 answers)
Closed 8 years ago.
Is there any chance to format a double value after an operation to come up with only 2 decimal values?
Because i am making a weather report and i'm getting the average of weather data per 5 minutes and i want to make it formal to have only 2 decimal places.
Which instead of having 10240.8999 i could have 10240.90.
Any thoughts or suggestion?
In .NET, the way to do this is:
var rounded = Math.Round(valueToRound, 2);
for decimal variables:
var rounded = Decimal.Round(valueToRound, 2);
Round with 2 decimals and convert to string to force 2 decimals
string rounded = String.Format("{0:f2}", Math.Round(10240.8999, 2));
Gives 10240.90

string.format() with at least one decimal place [duplicate]

This question already has answers here:
Display Float as String with at Least 1 Decimal Place
(4 answers)
Closed 7 years ago.
From the following two - double precision - numbers:
123456
0.0003232
I need to get (at least one decimal place) ...
123456.0 (one decimal place added)
0.0003232 (same as above)
... and never scientific notation, like E+000. The more close result from standard string.Format() is string.Format("{0:F1}", myDoubleVal) but in the second case the decimals are lost.
What else can I try?
Thanks.
Try the below
string.Format("{0:0.0###########}",myval);
Thanks

Categories

Resources