File tree Expand file tree Collapse file tree 4 files changed +51
-0
lines changed
1_BundlingVariablesIntoTuples
MovieReviewNormalisation.playground
MovieReviewNormalisationUsingLabels.playground Expand file tree Collapse file tree 4 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ import Foundation
3+
4+ func normalisedStarRating( forRating rating: Float ,
5+ ofPossibleTotal total: Float ) -> ( Int , String ) {
6+
7+ let fraction = rating / total
8+ let ratingOutOf5 = fraction * 5
9+ let roundedRating = round ( ratingOutOf5) // Rounds to the nearest integer.
10+ let numberOfStars = Int ( roundedRating) // Turns a Float into an Int
11+ let ratingString = " \( numberOfStars) Star Movie "
12+ return ( numberOfStars, ratingString)
13+ }
14+
15+ let ratingValueAndDisplayString = normalisedStarRating ( forRating: 5 , ofPossibleTotal: 10 )
16+
17+ let ratingValue = ratingValueAndDisplayString. 0
18+ print ( ratingValue) // 3 - Use to show the right number of stars
19+
20+ let ratingString = ratingValueAndDisplayString. 1
21+ print ( ratingString) // "3 Star Movie" - Use to put in the label
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" standalone =" yes" ?>
2+ <playground version =' 5.0' target-platform =' ios' >
3+ <timeline fileName =' timeline.xctimeline' />
4+ </playground >
Original file line number Diff line number Diff line change 1+
2+ import Foundation
3+
4+ func normalisedStarRating( forRating rating: Float ,
5+ ofPossibleTotal total: Float )
6+ -> ( starRating: Int , displayString: String ) {
7+
8+ let fraction = rating / total
9+ let ratingOutOf5 = fraction * 5
10+ let roundedRating = round ( ratingOutOf5) // Rounds to the nearest integer.
11+ let numberOfStars = Int ( roundedRating) // Turns a Float into an Int
12+ let ratingString = " \( numberOfStars) Star Movie "
13+ return ( starRating: numberOfStars, displayString: ratingString)
14+ }
15+
16+ let ratingValueAndDisplayString = normalisedStarRating ( forRating: 5 , ofPossibleTotal: 10 )
17+
18+ let ratingValue = ratingValueAndDisplayString. starRating
19+ print ( ratingValue) // 3 - Use to show the right number of stars
20+
21+ let ratingString = ratingValueAndDisplayString. displayString
22+ print ( ratingString) // "3 Star Movie" - Use to put in the label
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" standalone =" yes" ?>
2+ <playground version =' 5.0' target-platform =' ios' >
3+ <timeline fileName =' timeline.xctimeline' />
4+ </playground >
You can’t perform that action at this time.
0 commit comments