# String Essentials

**URL:** <https://forums.swift.org/t/string-essentials/21909>\
**Category:** Discussion\
**Tags:** string\
**Created:** [March 22, 2019, 1:32am UTC](https://forums.swift.org/t/string-essentials/21909 "2019-03-22T01:32:14Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![Michael\_Ilseman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_ilseman/32/44640_2.png) [@Michael\_Ilseman](https://forums.swift.org/u/Michael_Ilseman)\
**Post date:** [April 23, 2019, 11:29pm UTC](https://forums.swift.org/t/string-essentials/21909/15 "2019-04-23T23:29:53Z")

</div>

Another example use for offsetting an index, from @Ray_Fix's [post](https://forums.swift.org/t/real-scientific-notation-with-string-interpolation/23222)

```swift
  mutating func appendInterpolation<T: BinaryFloatingPoint>(_ value: T, precision: Int = 3) {
    var result = String(describing: value)
    defer { appendInterpolation(result) }

    guard precision > 0 else { return }

    let string = result
    guard let index = string.firstIndex(of: ".") else { return }

    let offset = precision+1
    let last = string.index(index, offsetBy: offset,
                            limitedBy: string.endIndex) ?? string.endIndex

    result = String(string[..<last])

    let fraction = string[index..<last].count

    if fraction < offset {
      result += String(repeating: "0", count: offset-fraction)
    }
  }

```

---

_[View the full topic](https://forums.swift.org/t/string-essentials/21909)._
