swift - Calling the built-in function with the same name as a custom function -
i have following code round value nearest number:
func round(_ value: double, tonearest nearest: double) -> double { let roundedvalue = round(value / nearest) * nearest return roundedvalue }
however, following complaint because use same name method builtin one:
missing argument parameter 'tonearest' in call
is there way around this? i.e. builtin round(value / nearest)
?
thanks.
as shown in following answer:
most darwin/c rounding methods readily available native swift methods, types conforming floatingpoint
(e.g. double
, float
). means if set on implementing own rounding method using same logic in question, can use rounded()
method of floatingpoint
, make use of .tonearestorawayfromzero
rounding rule, (as described in linked answer) equivalent darwin/c round(...)
method.
applied modify custom round(_:tonearest:)
function:
func round(_ value: double, tonearest nearest: double) -> double { return (value / nearest).rounded() * nearest }
Comments
Post a Comment