4

I want to extract 1 decimal float number from existing float number.

I have done this in Objective-C: Make a float only show two decimal places

Any idea how to make it happen in Swift?

Community
  • 1
  • 1
aksani56
  • 606
  • 2
  • 6
  • 14

3 Answers3

9

You can make the same in swift :

var formatter : NSString = NSString(format: "%.01f", myFloat)

Or like you want in one line :

println("Pro Forma:- \n Total Experience(In Years) = "+(NSString(format: "%.01f", myFloat)))

This work too with the old NSLog (but prefer println) :

NSLog("Pro Forma:- \n Total Experience(In Years) = %.01f \n", myFloat)
jaumard
  • 8,202
  • 3
  • 40
  • 63
  • thats technically correct, but how can i print this without adding one more line for string. Ex the below code wont compile: – aksani56 Jun 13 '14 at 07:19
  • println("Pro Forma:- \n Total Experience(In Years) = \(NSString(format: "%.01f", experience))") // Here experience is float which i want to be 1-decimal place. (This was possible in objective-C) – aksani56 Jun 13 '14 at 07:20
  • This complile for me println("Pro Forma:- \n Total Experience(In Years) = "+(NSString(format: "%.01f", experience))) – jaumard Jun 13 '14 at 07:32
2

You can try this

var experience = 10.25
println("Pro Forma:- \n Total Experience(In Years) = " + NSString(format: "%.01f", experience))
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
2

How about an infix operator?

// Declare this at file level, anywhere in you project. 
// Expressions of the form 
//     "format string" %% doubleValue
// will return a string. If the string is not a well formed format string, you'll
// just get the string back! If you use incorrect format specifiers (e.g. %d for double)
// you'll get 0 as the formatted value.
operator infix %% { }
@infix func %% (format: String, value: Double) -> String {
    return NSString(format:format, value)
}

// ...
// You can then use it anywhere
let experience = 1.234
println("Pro Forma:- \n Total Experience(In Years) = %.01f" %% experience)

I've tried to do it with generics, but I can't see how. To make it work with multiple types, just overload it for those types - e.g.

operator infix %% { }
@infix func %% (format: String, value: Double) -> String {
    return NSString(format:format, value)
}
@infix func %% (format: String, value: Float) -> String {
    return NSString(format:format, value)
}
@infix func %% (format: String, value: Int) -> String {
    return NSString(format:format, value)
}
Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • Interesting! However, it will really help if you can add comments against the code written by you. – aksani56 Jun 13 '14 at 07:55
  • Which part needs commented? I'm trying to make it work using generics at the moment, and will comment it properly when I'm finished! – Grimxn Jun 13 '14 at 07:58
  • I've just started with Swift. Can you explain the @infix directive? – aksani56 Jun 13 '14 at 08:00
  • Do you have The Book? It's free, and pretty much essential. Chapter is called "Operator Functions", or just use iBook search to look for @infix. – Grimxn Jun 13 '14 at 08:23
  • Ya i've the book from Apple, will have a look at that portion. Well, the comments are descriptive enough to catch. Thanks! – aksani56 Jun 13 '14 at 08:27