0

What should I add to my code to make return value 15.58, without using any library ?

def solution(side):
  result = (side**2)*(3**(0.5))/4
  return result

# return = 15.5885
  • maybe you should add the name of the language to your code – Andrey Tyukin Feb 17 '18 at 16:01
  • I meant in python – Çağdaş Cem Özkan Feb 17 '18 at 16:04
  • Your question is not clear. Do you mean you want to round the result to the nearest hundredth? What kind of rounding to you want--up, down, 4/5, banker's/symmetric, or you don't care? Which particular result do you want for your example: `15.58` or `15.59`? Do you need the answer to be exact or will the usual floating-point values (which will be very near the value you seek but probably not exactly equal to it) suffice? Are you talking about returning a particular value or printing it to look the particular way? – Rory Daulton Feb 17 '18 at 16:05
  • when i use; def solution(side): result = (side**2)*(3**(0.5))/4 result = round(result,2) return result it gives result as 15.59 Actually I want to learn how to round up and down, but if we consider this case i want it to be rounded down – Çağdaş Cem Özkan Feb 17 '18 at 16:07
  • Please look up rounding in Python. – Norrius Feb 17 '18 at 16:28
  • I did but, Im trying to make this code work in web browser because it's mini puzzle in www.codela.io. The main issue is, the example question has to be solved without library. Adding library is forbidden and the exact value has to be rounded down. @Norrius – Çağdaş Cem Özkan Feb 17 '18 at 16:35
  • I want it to be round down without using any library @RoryDaulton – Çağdaş Cem Özkan Feb 17 '18 at 16:43

3 Answers3

1
def solution(side):
   result = (side**2)*(3**(0.5))/4
   return round(result,2)

# return = 15.59

Rafaelox
  • 56
  • 1
  • 4
1

Original result value: 15.5884572681

Use floor to round down to get 15.58:

import math
def solution(side): 
    result = (side**2)*(3**(0.5))/4 
    return math.floor(result*100)/100
print(solution(6))  # prints 15.58

Use round with precision 2 to get 15.59:

def solution(side): 
    result = (side**2)*(3**(0.5))/4 
    return round(result,2)
print(solution(6))  # prints 15.59
Austin
  • 25,759
  • 4
  • 25
  • 48
0

Use round to ceil value :

# First we take a float and convert it to a decimal
result = (side**2)*(3**(0.5))/4

# Then we round it to 2 places
output = round(result,2)
print output

You can use math.floor to get 15.58 :

import math
result = (math.floor(result * 100)) / 100.0// 15.58
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38