How to convert a string to a number in Vue
Asked Answered
L

5

5

This string returns the value: Pressure 770.3157279 mm

<div class="info">Pressure {{info.main.pressure * 0.750064}} mm </div>

How do I make the number become an integer and get: Pressure 770 mm

Laurence answered 14/1, 2020 at 13:52 Comment(0)
I
10

You can use parseInt

<div class="info">Pressure {{ parseInt(info.main.pressure * 0.750064) }} mm </div>
Inspectorate answered 14/1, 2020 at 13:55 Comment(0)
D
3

To convert any value to an integer, just wrap it into:

parseInt(yourValue)

You are ready to go.

Disrelish answered 10/11, 2021 at 17:11 Comment(1)
"0x20" would parse to 32 and "15e3" to "15000" among others. You have to specify the radix: parseInt(yourValue, 10) at the very least.Protuberancy
N
2

To convert any value to an integer, just wrap it into:

Number(yourValue);
Nichollenicholls answered 6/7, 2022 at 1:25 Comment(1)
"0x20" would parse to 32 and "15e3" to "15000"Protuberancy
U
1

You can do it like this:

<div class="info">Pressure {{(info.main.pressure * 0.750064).toFixed() }} mm </div>
Unconsidered answered 14/1, 2020 at 13:56 Comment(0)
P
0

In the code of Vue 2.7, the v-model.number modifier casts values to numbers like this:

function toNumber(val) {
    var n = parseFloat(val);
    return isNaN(n) ? val : n;
}
Protuberancy answered 27/6, 2023 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.