> For the complete documentation index, see [llms.txt](https://pakornpat.gitbook.io/ios-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pakornpat.gitbook.io/ios-app/swift/number-and-string.md).

# Number & String

### ตัวเลข

ตัวแปรชนิดตัวเลขที่ใช้ใน Swift ค่อนข้างจะเหมือนกับภาษาอื่น คือ&#x20;

`Int` แทน interger หรือจำนวนเต็ม

{% hint style="info" %}
`Int` ในภาษานี้จะเป็นแบบ 64 bit หรือ 32 bit ขึ้นอยู่กับ platform และใน iOS จะต้องระวังในกรณีที่มีการใช้ตัวเลขขนาดใหญ่ เช่น ใน iPhone รุ่นเก่าที่เป็น 32 bit การใช้ `Int` จะเป็นแบบ 32 bit ซึ่งอาจจะ overflow ได้

บางครั้งเราจึงจะต้องระบุชัดเจนว่า `Int32` หรือ `Int64` ไปเลยเพื่อความปลอดภัย

iPhone รุ่นสุดท้ายที่เป็น 32 bit คือ iPhone 5C
{% endhint %}

`UInt` แทน Unsigned integer

`Double` แทนเลขที่มีทศนิยมเก็บแบบ 64 bit

`Float` ก็แทนเลขทศนิยมเช่นเดียวกับ `Double` แต่เก็บแบบ 32 bit

{% hint style="info" %}
เราสามารถใช้ \_ ช่วยในการเขียนค่าตัวเลขได้เพื่อช่วยให้เราสามารถอ่านได้ง่ายขึ้น แต่ไม่มีผลต่อการทำงาน เช่น

```swift
var money = 10_000_000
```

{% endhint %}

{% hint style="info" %}
ในการ casting ค่า เราสามารถทำแบบนี้ได้

```swift
let amount = 20.5
let number = Int(amount)
let rounded = Int(amount.rounded())
```

{% endhint %}

### ข้อความ

สำหรับข้อความเราจะใช้ตัวแปรประเภท `String` ในการเก็บค่า โดยข้อความที่เก็บจะอยู่ภายใต้เครื่องหมาย `"` เช่นที่เราเขียนกันไปแล้ว

```swift
let name = "pop"
```

ในกรณีที่เราต้องการเขียนแบบหลายบรรทัดเราสามารถเขียนได้โดยใช้ `"""` แทน เช่น

```swift
let message = """
This is multiline message
without to concat multiple string
but content must begin on new line 
"""
```

เราสามารถพิมพ์ออกมาดูที่ console ได้ด้วยคำสั่ง `print`&#x20;

```swift
print("Hello, world!")
print(name)
```

`String` สามารถนำมาต่อกันด้วย `+` ได้เลย

```swift
print("Hello, " + name)
```

เราสามารถนำตัวแปรมาฝังไว้ในข้อความโดยใส่ตัวแปรภายใต้ `\()` แบบนี้เลยก็ได้&#x20;

```swift
print("Hello, \(name).")
```

หรือใน multi-line string แบบนี้

```swift
let quotation = """
I said "Hello, \(name)."
"""
```

แต่สิ่งที่ `String` ใน Swift ไม่เหมือนภาษาอื่นคือ เราไม่สามารถอ้างถึง index `Character` ในข้อความด้วย `Int` แบบที่เราใช้ใน `Array` ได้ เช่นตัวอย่างข้างล่างนี้จะ Error

```swift
let char = name[1]
// 'subscript(_:)' is unavailable
```

ใน Swift เราจะต้องใช้ Type `String.Index` ในการอ้าง ด้วยโค้ดแบบนี้

```swift
let index = name.index(name.startIndex, offsetBy: 1)
let char = name[index]
// char = "o"
```

นอกจากนี้เรายังสามารถหาความยาวของข้อความได้ด้วย property `count` และตรวจสอบได้ว่าข้อความว่างหรือไม่ด้วย `isEmpty`

```swift
let nameLength = name.count
let isNameEmpty = name.isEmpty
```

{% hint style="info" %}
เราสามารถเปลี่ยนตัวเลขเป็นข้อความได้แบบง่าย ๆ แบบนี้

```swift
let cost = "$" + String(rounded)
```

ในกรณีซับซ้อนเราสามารถใช้ `NumberFormatter`ในการแปลงค่าได้

```swift
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2

formatter.string(from: 234.456)
```

อ่านเพิ่มเติมได้ที่ [Apple NumberFormatter](https://developer.apple.com/documentation/foundation/numberformatter)
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pakornpat.gitbook.io/ios-app/swift/number-and-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
