vue.js numeric and string conversion
Numeric to String Conversion:
// Numeric to String Conversion
let numericValue = 123;
let stringValue = `${numericValue}`; // String interpolation
// Or
let stringValue = numericValue.toString(); // Using toString() method
String to Numeric Conversion:
// String to Numeric Conversion
let stringValue = "123";
let numericValue = parseInt(stringValue); // For integers
// Or
let floatValue = parseFloat(stringValue); // For floats
Rounding:
let number = 10.6;
let rounded = Math.round(number); // Rounds to the nearest integer
// Output: 11
let roundedDown = Math.floor(number); // Rounds down to the nearest integer
// Output: 10
let roundedUp = Math.ceil(number); // Rounds up to the nearest integer
// Output: 11
let number = 10.6789;
let decimalPlaces = 2;
let rounded = Math.round(number * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
// Output: 10.68
Template Example:
<template>
<div>
<p>Numeric to String Conversion: {{ numericValue.toString() }}</p>
<p>String to Numeric Conversion: {{ parseInt(stringValue) }}</p>
<p>Rounded Number: {{ Math.round(number) }}</p>
</div>
</template>
<script>
export default {
data() {
return {
numericValue: 123,
stringValue: "123",
number: 10.6
};
}
};
</script>
Result:
<p>Numeric to String Conversion: 123</p>
<p>String to Numeric Conversion: 456</p>
<p>Rounded Number: 11</p>
'VUE' 카테고리의 다른 글
window 에서 node 버전 바꾸기 nvm (0) | 2024.05.22 |
---|---|
vue v-calendar v-date-picker 년도 월 선택 팝업 추가 (0) | 2024.03.27 |
vue 자식컴포넌트 object 타입 선언 및 초기화 (0) | 2023.09.20 |
vue3 firebase fileupload download (0) | 2023.09.17 |
localStorage sessionStorage set get remove (0) | 2023.05.21 |