0

Is there a way to get the value after the decimal point out of a double variable?

doubleVar = 234.045

Can i get only the .045 out of the variable?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Tobi
  • 67
  • 4

2 Answers2

1

Divide the double by 1 using truncatingRemainder(dividingBy:) and get the reminder.

var doubleVar = 234.045
var new = doubleVar.truncatingRemainder(dividingBy: 1.0)
let rounded = Double(round(1000*new)/1000)
print(rounded)

OR

Using C function modf

var doubleVar = 234.045
let splitPi = modf(doubleVar)
splitPi.0 // 324.0
splitPi.1 // 0.045
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
0

I don't know swift but integer truncation should help. Something like

let doubleVar = 234.045

let result = doubleVar - Int(doubleVar)