Math Round in VS C# [duplicate] - c#

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}");

Related

I'm trying to make c# calculation but it's wrong [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed 2 years ago.
I'm currently making a new project on c# and i have a problem - calculations are wrong. My calculation is 1 * (1 + (1 - 160 / 180)) but it returns 2. I checked this in my calculator and the right answer is 1.11111...
Please help!
P. S. sorry for my bad english
160 / 180 is integer division and will result in 0.
Try using 160.0/180 in order to promote the result a double.

C# Remove certain amount of numbers end of line [duplicate]

This question already has answers here:
How to limit a decimal number? [duplicate]
(6 answers)
Closed 6 years ago.
So,
I got example number: 0.00857382942837523 which length is 18, but I would like to remove from the end example 9 numbers. Thing is, that number is different every time, so I can't "predict" numbers what are going to be at the end.
//My question was marked to duplicate, but it can't be solved like that. Mine has 0.00, if I would do math.round it would give up 0...
You need Math.Truncate
decimal d= 0.00857382942837523m;
d= Math.Truncate(d * 1000000000m) / 1000000000m;
Console.WriteLine(d); // 0.008573829

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

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")

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

Format to two decimal places [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
I have a number
long n = 32432432423;
I want to divide this by 1450 and print on a console with 2 decimal places (rounded)
How can I do it?
COnsole.WriteLine(????);
Console.WriteLine("{0:N2}", ((double)n) / 1450);
Console.WriteLine("{0:0.00}", 32432432423 / 1450.0);
make use of Math.Round
Console.WriteLine( Math.Round(Convert.ToDecimal(32432432423 / 1450.0), 2));
Result: 22367194.77

Categories

Resources