admin管理员组文章数量:1021167
I want to convert a number to string in JS, which is the best and remaded way to do that?
I have tried the method shared on W3school so far. but want to know the remaded methods from experts
I want to convert a number to string in JS, which is the best and remaded way to do that?
I have tried the method shared on W3school so far. but want to know the remaded methods from experts
Share Improve this question asked Jan 7, 2023 at 3:20 Shajidur Rahman BappiShajidur Rahman Bappi 211 silver badge3 bronze badges 2- 2 use the Number.prototype.toString method - I'm curious, what IS the method shared on W3school? Knowing W3school, it's probably wrong - I would avoid that site like the joke it is, and get better documentation form MDN – Jaromanda X Commented Jan 7, 2023 at 3:23
-
personally, I always use
toString
since you can specify aradix
– Jaromanda X Commented Jan 7, 2023 at 3:38
4 Answers
Reset to default 2All of these will work:
`${num}`
num.toString()
'' + num
String(num)
Seems that var str = '' + num
is the fastest way to convert a number to a string (num being the number variable).
let num = 5
var str = '' + num
(results based on this benchmark: http://jsben.ch/#/ghQYR)
You can use the built in function: toString():
Example:
let number = 3
let stringed_number = number.toString()
I remend using .toLocaleString if you want pretty formatting, like mas or periods for grouping.
However if you know you don't want pretty formatting you should use .toString
I want to convert a number to string in JS, which is the best and remaded way to do that?
I have tried the method shared on W3school so far. but want to know the remaded methods from experts
I want to convert a number to string in JS, which is the best and remaded way to do that?
I have tried the method shared on W3school so far. but want to know the remaded methods from experts
Share Improve this question asked Jan 7, 2023 at 3:20 Shajidur Rahman BappiShajidur Rahman Bappi 211 silver badge3 bronze badges 2- 2 use the Number.prototype.toString method - I'm curious, what IS the method shared on W3school? Knowing W3school, it's probably wrong - I would avoid that site like the joke it is, and get better documentation form MDN – Jaromanda X Commented Jan 7, 2023 at 3:23
-
personally, I always use
toString
since you can specify aradix
– Jaromanda X Commented Jan 7, 2023 at 3:38
4 Answers
Reset to default 2All of these will work:
`${num}`
num.toString()
'' + num
String(num)
Seems that var str = '' + num
is the fastest way to convert a number to a string (num being the number variable).
let num = 5
var str = '' + num
(results based on this benchmark: http://jsben.ch/#/ghQYR)
You can use the built in function: toString():
Example:
let number = 3
let stringed_number = number.toString()
I remend using .toLocaleString if you want pretty formatting, like mas or periods for grouping.
However if you know you don't want pretty formatting you should use .toString
本文标签: type conversionhow to convert number to string in JavaScriptStack Overflow
版权声明:本文标题:type conversion - how to convert number to string in JavaScript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745534749a2154909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论