CarveDrone
CarveDrone

Reputation: 915

Finding How Many Numbers are in a Double using Swift

I have put together an application that uses Cramer's rule to fine the solution to three variable systems. It works great, but in some cases the answers returned are too big to fit in the answers spot. There must be a simple way to figure out the length of a double, then limit it, I just can't seem to find it.

var x:Double = dx/d
var y:Double = dy/d
var z:Double = dz/d
return (x,y,z)

The code above shows the end of the function that calculates the points, which isn't really necessary.

So in summary: is there an easy way to determine the length (amount of numbers), in a double?

Thanks for all your help!

Upvotes: 0

Views: 1245

Answers (1)

Connor
Connor

Reputation: 64674

If you just need to show a certain number of digits for presenting, you can use a format string.

String(format: "%3.1f", 1.111) // 1.1

The 3 specifies how many characters in the number, while the 1 specifies how many digits after the decimal point.

Upvotes: 1

Related Questions