Skip to content

Instantly share code, notes, and snippets.

@DJNgoma
Last active January 4, 2018 03:08
Show Gist options
  • Save DJNgoma/7709829da0a4b2643d57 to your computer and use it in GitHub Desktop.
Save DJNgoma/7709829da0a4b2643d57 to your computer and use it in GitHub Desktop.
Rounding to the nearest 5 cents in Swift or C
// Basic C, but being used in a Swift project of mine.
// Just a simple way of getting any floating number to round up or down to a 5 cent figure for calulations.
let x = 45.23
let y = 23.58
let z = 82.12
a = round(x/0.05)*0.05
b = round(y/0.05)*0.05
c = round(z/0.05)*0.05
print(a) // 45.25
print(b) // 23.60
print(c) // 82.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment