0

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!

CarveDrone
  • 915
  • 1
  • 9
  • 23

1 Answers1

1

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.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • 1
    This is 90% correct - you aren't specifying the number of digits before the decimal with that first 1, you're specifying the minimum amount of space the whole number (including decimal point and trailing digits) should take. If you feed 1000.0001 into that format, you'll get "1000.0" out of it, not "0.0" or similar. – Nate Cook Oct 18 '14 at 03:13