0
Why is Sololearn rounding up numbers?
I was writing some code to solve a challenge and it's crucial to have the double variable with its decimal place but when i run the code it gives me only the integer without the decimal places, I didn't ask to round up but it still doing it. For example i need the Math.Ceiling(31/7), it gaved me 4 instead of 5. (31/7 = 4.3 and the ceiling is 5)
8 Réponses
+ 4
AJ #Learn With Me 👨💻 correction in your statement, it should be
"Ceiling method returns nearest whole number GREATER THAN THE ARGUMENT PASSED so if there is value 4.3, Ceiling method will give 5.0"
+ 3
Pepe like I said if you do
`double z = 31.0/7.0`
that will give you a double. Dividing integers by integers gives integers in C#.
+ 2
The Math.Ceiling method returns the smallest integral value greater than equal to the argument passed
https://docs.microsoft.com/en-us/dotnet/api/system.math.ceiling?view=net-5.0#System_Math_Ceiling_System_Double_
Don't use Math.Ceiling method if you don't want int value. Just use 31.0/7.0.
+ 1
Oh ok i see thank you both of you. 😀
0
Nvm I didn't say anything, the value i wanted was 5 not 4.3, sorry, i didn't write it correctly.
0
Pepe
Ceiling method returns nearest whole number so if there is value 4.3, Ceiling method will give 5.0.
if 4.1 then 5.0
if 4.8 then 5.0
0
Well Yes but my issue was when i put in a variable it will still give me a round up number. Like "double z = 31/7;" it will give 4 instead of 4.3 making not usable for Math.Ceiling because it uses the value of 4 instead of 4.3 and the ceiling of that 4 is 4.
0
Pepe You have to do like this to get fractional value.
double x = (double) 31 / 7;