> For the complete documentation index, see [llms.txt](https://mr-poston-1.gitbook.io/c++/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mr-poston-1.gitbook.io/c++/1.-c++-basics/1.2-basic-data-types/1.2.3-numbers.md).

# 1.2.3 Numbers

Like other languages, C++ has several different variable types for numbers. In this course, you will use two basic types, integer (`int`) to represent whole numbers and doubles (`double`) to represent decimal numbers.

### Declaring and Initializing Numbers

Like Java, you can declare and initialize in one step, or declare and initialize later.

```
int num1 = 10;

double num2;
num2 = 11.5;
```

C++ allows you to declare and assign multiple values at once.

```
int num3, num4;
num3 = num4 = 15;
```

### A Word of Caution

C++ as a language has fewer checks compared to Java or other languages. As you saw with accessing a string index that did not exist, C++ will also allow you to access a variable that has not been initialized. Other languages such as Java will throw an error if you try to access a variable before it has been initialized, but this is not the case in C++. When a variable is declared, a memory location is associated with that variable. If you access that variable before initializing it, you will get the last value stored in that memory location, which will not have any meaning to you.

### Math with Integers and Doubles

Math operations work exactly the same as Java. A calculation with two integers results in a truncated integer:

* **Example:** 15 / 10 → 1

A calculation with at least 1 double results in a double

* **Example:** 15.0 / 10 → 1.5

### Casting Numbers in C++

Casting numbers work exactly the same as Java. To cast a value, you include the type that you want to cast to inside of parentheses. When a value is cast, it creates a temporary version of the value in the new format, but does not change the original value.

```
int i = 5;
double d = (double) i;
int i2 = (int) d;
```

### Casting To and From String

Casting strings to numbers uses a series of `sto` commands. The two most common that you will use are listed here:

* `stoi(num)` - converts string to int
* `stod(num)` - converts string to double

C++ doesn’t allow implicit conversion from a number to a string. In order to use a number as a string, the `to_string` function needs to be explicitly called.

For example, if you want to concatenate a string and a number, you would do it like this:

```
int limit = 65;
string sign = "Speed Limit: " + to_string(limit);
```

### Try This Example

{% embed url="<https://onlinegdb.com/2mSQ35KtT>" %}
Numbers
{% endembed %}


---

# 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://mr-poston-1.gitbook.io/c++/1.-c++-basics/1.2-basic-data-types/1.2.3-numbers.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.
