Swift get Fraction of Float

Malte

I've been trying out swift lately and i've come across a rather simple Problem.

In Obj-C when i want to get the fraction digits of a float i'd do the following:

float x = 3.141516
int integer_x = (int)x;
float fractional_x = x-integer_x;
//Result: fractional_x = 0.141516

in Swift:

let x:Float = 3.141516
let integerX:Int = Int(x)
let fractionalX:Float =  x - integerX 

-> this results in an error because of mismachting types

Any Idea how to do it correctly?

Thanks in Advance

Malte

Greg

The problem is that you cannot subtract Float and Int, you should convert one of this value to be the same as another, try that:

let fractionalX:Float = x - Float(integerX)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Float to Fraction conversion in Python

From Dev

Convert fraction to float in PHP

From Dev

angular float number fraction down

From Dev

Convert a float to an approximate fraction in javascript

From Dev

Float data without fraction part

From Dev

Get raw bytes of a float in Swift

From Dev

Decimal to Fraction conversion in Swift

From Dev

Java get fraction method

From Dev

How to turn decimal into a fraction - Swift

From Dev

How to extract dyadic fraction from float

From Dev

Converting a mixed fraction to a float number in Netezza

From Dev

How to get 1 decimal Float in swift

From Dev

Cant convert Swift CMTimeRange to Float or get the number?

From Dev

How to get currency FRACTION name?

From Dev

unable to get fraction of seconds using strptime()

From Dev

Shell script to get each line as fraction of total

From Dev

xpages - Get rid of the fraction part of a currency

From Dev

unable to get fraction of seconds using strptime()

From Dev

Shell script to get each line as fraction of total

From Dev

how to get fraction as it is ( without getting itssimplest fraction )? i vainly tried using this:

From Dev

how to get fraction as it is ( without getting itssimplest fraction )? i vainly tried using this:

From Java

Convert Float to Int in Swift

From Java

Convert String to Float in Swift

From Java

Casting CGFloat to Float in Swift

From Dev

Swift: NSNumber is not a subtype of Float

From Dev

Leading zeros for float in Swift

From Dev

Float divison and casting in Swift

From Dev

Swift: "float is not convertible to int"

From Dev

Swift float multiplication error