1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

doc: add info about _ separator in literals (#5823)

This commit is contained in:
Swastik Baranwal
2020-07-14 19:16:13 +05:30
committed by GitHub
parent c3ec5323f0
commit c3bdacbf04
2 changed files with 42 additions and 0 deletions

View File

@ -388,18 +388,22 @@ println(s) // "hello\nworld"
```v
a := 123
```
This will assign the value of 123 to `a`. By default `a` will have the
type `int`.
You can also use hexadecimal notation for integer literals:
```v
a := 0x7B
```
... or binary notation for integer literals:
```v
a := 0b01111011
```
... or octal notation for specifying integer literals:
```v
a := 0o173
```
@ -407,8 +411,18 @@ a := 0o173
All of these will assign the same value 123 to `a`. `a` will have the
type `int` no matter what notation you have used for the integer literal.
V also supports writing numbers with `_` as separator:
```v
num := 1_000_000 // same as 1000000
three := 0b0_11 // same as 0b11
float_num := 3_122.55 // same as 3122.55
hexa := 0xF_F // same as 255
oct := 0o17_3 // same as 0o173
```
If you want a different type of integer, you can use casting:
```v
a := i64(123)
b := byte(42)
@ -416,6 +430,7 @@ c := i16(12345)
```
Assigning floating point numbers works the same way:
```v
f := 1.0
f1 := f64(3.14)