1.2 Stored Value vs Displayed Value
ป้ายราคาเขียน “฿199” แต่ระบบหลังบ้านเก็บแค่
199กับข้อมูลว่า “สกุลเงินบาท” ถ้าคุณเปลี่ยนสกุลเงินเป็นดอลลาร์ ตัวเลขยังคงเป็น 199 — มันแค่เปลี่ยน “เสื้อผ้า” ไม่ได้เปลี่ยนตัวตน
คิดแบบนี้
Section titled “คิดแบบนี้”ลองนึกภาพ คน กับ เสื้อผ้า:
- Stored value = ตัวคนจริงๆ (ไม่เปลี่ยนไม่ว่าจะใส่อะไร)
- Displayed value = เสื้อผ้าที่ใส่ (เปลี่ยนได้ตลอด)
- Format = กฎว่า “ให้ใส่ชุดแบบนี้”
ตัวอย่าง: เซลล์เก็บ 45372 (ตัวเลข) → format เป็น “Date” → แสดงเป็น 24 เม.ย. 2024
เปลี่ยน format เป็น “Number” → แสดงเป็น 45372 อีกครั้ง → ค่าไม่เคยเปลี่ยน
ทดลอง 1: ราคา
Section titled “ทดลอง 1: ราคา”- พิมพ์
199ในเซลล์ A1 - ไปที่ Format → Number → Currency → แสดงเป็น
฿199.00 - ไปที่ Format → Number → Number → แสดงเป็น
199.00 - ไปที่ Format → Number → Percent → แสดงเป็น
19900.00%
ลองใช้ =A1+1 → ได้ 200 — ค่าข้างในยังเป็นตัวเลข 199
ทดลอง 2: วันที่
Section titled “ทดลอง 2: วันที่”- พิมพ์
2026-04-24ในเซลล์ B1 → Sheets auto-detect เป็นวันที่ - ลองใช้
=N(B1)→ ได้ตัวเลขประมาณ46141(serial number ของวันที่) - เปลี่ยน format เป็น “Number” → เห็น serial number ตรงๆ
- เปลี่ยนกลับเป็น “Date” → เห็นวันที่เดิม
ค่าที่เก็บคือตัวเลข — Sheets แค่ “แต่งตัว” ให้ดูเหมือนวันที่
ทดลอง 3: จับโกหก
Section titled “ทดลอง 3: จับโกหก”- พิมพ์
฿199(พิมพ์สัญลักษณ์บาทเอง) ในเซลล์ C1 - ลองใช้
=ISNUMBER(C1)→ FALSE — มันเป็น text ไม่ใช่ตัวเลข! =C1+1→ Error!
ถ้าพิมพ์สัญลักษณ์สกุลเงินเอง Sheets เก็บเป็น text — ไม่ใช่ตัวเลขที่ format เป็น currency
ใน C ไม่มี concept ของ “display format” แยกจาก stored value — programmer เลือก format เองตอน print:
#include <stdio.h>
int main() { double price = 199.0;
// ค่าที่เก็บเหมือนกัน แต่แสดงต่างกัน printf("Plain: %f\n", price); // 199.000000 printf("2 dec: %.2f\n", price); // 199.00 printf("Currency: ฿%.2f\n", price); // ฿199.00 printf("Sci: %e\n", price); // 1.990000e+02 printf("Integer: %d\n", (int)price); // 199
// วันที่ = ไม่มี date type ใน C int days_since_epoch = 20567; // สมมติ printf("Stored: %d\n", days_since_epoch); // ต้องคำนวณเอง หรือใช้ library เพื่อแสดงเป็นวันที่ return 0;}Key insight: ใน C ค่าที่เก็บคือ bits ในหน่วยความจำ การแสดงผลขึ้นอยู่กับ format string (%f, %d, %e) ทั้งหมด — stored value ไม่เปลี่ยนเลย
import pandas as pdfrom datetime import datetime
# ราคา — ค่าเดียวกัน แสดงต่างกันprice = 199.0print(f"{price}") # 199.0print(f"{price:.2f}") # 199.00print(f"฿{price:,.2f}") # ฿199.00print(f"{price:.0f}") # 199print(f"{price:e}") # 1.990000e+02# ทุก print ข้างบน — price ยังเป็น 199.0 เสมอ
# วันที่ — stored value เป็น object, displayed value เปลี่ยนได้dt = pd.to_datetime('2026-04-24')print(type(dt)) # <class 'pandas._libs.tslibs.timestamps.Timestamp'>print(dt) # 2026-04-24 00:00:00print(dt.strftime('%d/%m/%Y')) # 24/04/2026print(dt.strftime('%B %d, %Y')) # April 24, 2026print(int(dt.timestamp())) # 1777017600 (Unix timestamp)Key insight: pd.to_datetime() แปลง string ให้เป็น Timestamp object จริงๆ — ไม่ใช่แค่เปลี่ยนหน้าตา แต่เปลี่ยน stored value จาก text เป็น datetime
-- ราคาSELECT 199.00 AS stored, TO_CHAR(199.00, 'FM999,999.00') AS formatted_number, TO_CHAR(199.00, 'FM฿999,999.00') AS formatted_currency;-- stored: 199.00 | formatted_number: 199.00 | formatted_currency: ฿199.00-- TO_CHAR ไม่เปลี่ยนค่าที่เก็บ แค่สร้าง text ใหม่
-- วันที่SELECT DATE '2026-04-24' AS stored_date, TO_CHAR(DATE '2026-04-24', 'DD/MM/YYYY') AS thai_format, TO_CHAR(DATE '2026-04-24', 'Month DD, YYYY') AS us_format, DATE '2026-04-24' - DATE '1970-01-01' AS days_since_epoch;-- stored_date: 2026-04-24-- thai_format: 24/04/2026-- us_format: April 24, 2026-- days_since_epoch: 20567
-- CAST เปลี่ยน stored value จริงSELECT CAST('199' AS INTEGER); -- text → integer (stored value เปลี่ยน!)SELECT CAST(199 AS VARCHAR(10)); -- integer → text (stored value เปลี่ยน!)Key insight: TO_CHAR() ไม่เปลี่ยนค่าที่เก็บ (แค่สร้าง text ใหม่) แต่ CAST() เปลี่ยนค่าที่เก็บจริงๆ — ระวังอย่าสับสน
Format vs Cast — ต่างกันอย่างไร
Section titled “Format vs Cast — ต่างกันอย่างไร”| การกระทำ | เปลี่ยน stored value? | ตัวอย่าง |
|---|---|---|
| Format | ไม่ | Sheets: Number → Currency, Python: f"{x:.2f}", SQL: TO_CHAR() |
| Cast | ใช่ | Sheets: =VALUE("199"), Python: int("199"), SQL: CAST('199' AS INTEGER) |