Skip to content

Commit 90784a8

Browse files
Update Type Conversion Topic
1 parent 1aad92e commit 90784a8

5 files changed

Lines changed: 71 additions & 5 deletions

File tree

docs/programming-languages/type-conversion/_c.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
// AVOID atoi() and its counterparts (Check More Info)
77

88
// Prefer strtol()
9-
// TODO: Examples
9+
char *endPointer;
10+
long convertedValue = strtol("123", &endPointer, 10);
11+
12+
if (*endPointer != '\0')
13+
{
14+
// Conversion did not consume the full string
15+
}
16+
17+
// Base 10
18+
long convertedValue = strtol("123", NULL, 10);
19+
20+
// Base 16
21+
long convertedValue = strtol("FF", NULL, 16);
1022

1123
```
1224

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
```Go
2-
// TODO
2+
// ------------------------------------
3+
// String to Number
4+
// ------------------------------------
5+
convertedValue, err := strconv.Atoi("100")
6+
convertedValue, err := strconv.ParseInt("100", 10, 64)
7+
convertedValue, err := strconv.ParseFloat("10.5", 64)
8+
9+
// ------------------------------------
10+
// Floating number to Integer
11+
// ------------------------------------
12+
convertedValue := int(math.Floor(5.5))
13+
convertedValue := int(math.Ceil(5.5))
14+
convertedValue := int(math.Round(5.5))
15+
16+
// ------------------------------------
17+
// Number to String
18+
// ------------------------------------
19+
convertedValue := strconv.Itoa(100)
20+
convertedValue := strconv.FormatInt(100, 10)
21+
convertedValue := strconv.FormatFloat(10.5, 'f', -1, 64)
322
```
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Rust
2-
// TODO
2+
// ------------------------------------
3+
// String to Number
4+
// ------------------------------------
5+
let converted_value: i32 = "100".parse().unwrap();
6+
let converted_value = "100".parse::<i32>().unwrap();
7+
8+
// ------------------------------------
9+
// Floating number to Integer
10+
// ------------------------------------
11+
let converted_value = 5.5_f64.floor() as i32;
12+
let converted_value = 5.5_f64.ceil() as i32;
13+
let converted_value = 5.5_f64.round() as i32;
14+
15+
// ------------------------------------
16+
// Number to String
17+
// ------------------------------------
18+
let converted_value = 100.to_string();
19+
let converted_value = format!("{}", 100);
320
```
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```Go
2-
// TODO
2+
// Go infers types with short variable declarations and var initialization.
3+
variableName := 10
4+
variableName := 10.5
5+
variableName := "text"
6+
7+
var variableName = 10
8+
var variableName = "text"
9+
10+
// Explicit type declaration is still available.
11+
var variableName int64 = 10
312
```
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```Rust
2-
// TODO
2+
// Rust infers local variable types from initialization and usage.
3+
let variable_name = 10; // i32 by default
4+
let variable_name = 10.5; // f64 by default
5+
let variable_name = "text"; // &str
6+
7+
// Explicit type annotation is still available.
8+
let variable_name: u64 = 10;
9+
10+
// Turbofish can guide generic inference.
11+
let numbers = Vec::<i32>::new();
312
```

0 commit comments

Comments
 (0)