> For the complete documentation index, see [llms.txt](https://cs.brash.ca/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs.brash.ca/unit-2/variables/numbers/converting-and-rounding.md).

# Converting & Rounding

[Strongly typed languages](https://en.wikipedia.org/wiki/Strong_and_weak_typing) require the programmer to select a variable type for numbers. For example, an `integer` is a whole number only such as -7 and 30. A `double` or `float` contains values after the decimal like 6.5 or -3.14.\
\
Javascript, on the other hand, treats any variable as a value or string interchangeably, including decimal values. It is a *weakly* or *dynamically* typed language.

### Converting \[String -> Num]

Let's say you are requesting input from the user. They are to type in a number representing the grade they are in and you will be adding 5 to guess their age. You prompt the user, add 5, and output the results... It would go a little like this:

```javascript
let grade = prompt("Please enter your current grade");
let output = grade + 5;
console.log(output);
```

The only problem is that the grade being entered by the user is coming back as a `String` and the `+` operator on strings means to concatenate them. Check it out:

{% embed url="<https://repl.it/repls/MediumorchidGruesomeAdware>" %}

To avoid this, we need to make sure our `grade` variable is being treated as a number. As always, there is more than one way to do this.

#### Number

The Number object in Javascript has many useful functions, not the least of which is returning a new Number if we pass it a string.\
`grade = Number(grade);    // Returns a numeric value of grade, if possible`

#### parseInt or parseFloat

Javascript also has two methods to parse (or convert) a string into a whole number value, `parseInt(string)`, or a decimal value, `parseFloat(string)`.\
`grade = parseInt(grade);    // Returns an integer representation of grade`

It should be noted that the preferred method is to use the `Number` object as it has better handling of non-numeric input.

### Converting \[Num -> String]

Going the other direction is decidedly simpler. Almost anything in Javascript has a `toString()` method which returns a string representation of that variable / object.

```javascript
let num = 15;
let fifteen = num.toString();
console.log(typeof num);     // number
console.log(typeof fifteen); // string
```


---

# 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, and the optional `goal` query parameter:

```
GET https://cs.brash.ca/unit-2/variables/numbers/converting-and-rounding.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
