+ 2
How to round down with decimals 2.3445 -> 2.344
I am trying to round down a number 2.3445 should become 2.344 I want to set a certain number of decimals (in this case 3) Math.Floor or Math.Truncate are returning integers. Math.Round seems to work. But the sololearn playground do ises not accept MidpointRounding.ToNegativeInfinity. Is MidpointRounding.ToNegativeInfinity the correct way to do it ? What should I use to round down a number ? https://code.sololearn.com/crDXEjn9a12c/?ref=app
11 Respuestas
+ 2
Shift the number up by the number of decimal places you want. Then truncate. Then shift back down.
Math.Truncate(x*1000.0)/1000.0
+ 3
By default .NET uses bankers rounding, for an explanation see this article
https://www.codeproject.com/Tips/1272542/How-NETs-Math-Round-has-Nothing-to-do-with-Maths-A
Also see the docs about all the different types of rounding and how to use them. (typically by adding another argument)
https://docs.microsoft.com/en-us/dotnet/api/system.math.round?view=net-6.0
Note when using the docs and are running the program in Sololearn then you need to use .NET framework which is deprecated.
+ 1
You can just use this :
Console.WriteLine(Math.Round(x, 3)); // ?
+ 1
That does give the correct result for 2.3445.
2.3448 however does become 2.345
This should also be rounded to 2.344
+ 1
Only MidpointRounding. ToEven, AwayFromZero seems to work.
MidpointRounding.ToZero may fit actually..
No idea about MidpointRounding.ToNegetiveInfinity
And rounding 2.3448 to 2.344 is not rounding , is it possible with round()? IDK..
+ 1
Okay I found now a Mathmatical way:
double calc = 2.3445;
Console.WriteLine(calc = Math.Truncate(calc*1000) / 1000);
works:
Math.Truncate(calc*1000) = int 2344 / 1000 = 2.344
Math.Truncate(calc*100) = int 234 / 100 = 2.34
+ 1
Adam McGregor
what do you mean by:
Note when using the docs and are running the program in Sololearn then you need to use .NET framework which is deprecated.
Is the .NET framework deprecated?
+ 1
Another way , hard code to using string.. Without round.
x = 2.3448;
Console.WriteLine(x.ToString().Substring(0,5));
+ 1
sneeze
Microsoft discontinued the .NET framework years ago (which was Windows specific) in favour of .NET Core which can run on most platforms (Windows, Mac, Ios, Android, and Linux)
.NET Core is now referred to as just .NET which is currently on version 6 with a new release schedule around the end of every year.
You need not worry too much since, Microsoft has a tendency to avoid breaking changes a much as possible to make upgrading easier (unlike other languages).
So your c# knowledge will still be perfectly applicable. And using Sololearn is fine whilst learning to program. However, there many great new features in newer versions.
If you are going to start any new app projects, using the latest version will be beneficial as they are faster and have more features to make building apps easier.
If you get a job you may find yourself using .NET framework, as many companies are slow to upgrade despite it being easy for most apps. There are some exceptions which make it harder though.
0
At that Point I would make that decimal to string. Then you could do:
string decimal = 2.3445.ToString();
Console.WriteLine("{0}", decimal.Length > 4? decimal.Remove(5): decimal);
edit: after that you could again parse it but I don't think that's the best Option.
0
You could also take the more annoying way with:
(double) ( (int)(calc*1000) ) /1000