Javascript - convert numbers to strings

How to convert a float or interger value to string using javascript?

The simplest way to convert any variable to a string is to add an empty string to that variable, for example:

a = a+'' // This converts a to string
b += '' // This converts b to string

Or you can use toString() function.
var a = 6.23;
a.toString();

This will return the string “6.23″.

The resultant string will hold the decimal representation of the original number.

For converting numbers to binary, octal, or hexadecimal strings (or to any other base) you can use, in JavaScript 1.1, the standard method Number.toString(radix) for converting a number to a string representing that number in a non-decimal number system (e.g. to a string of binary, octal or hexadecimal digits). For example:

a = (50274).toString(16) // this gives "c462"
b = (76).toString(8) // this gives "114"
c = (7623).toString(36) // this gives "5vr"
d = (100).toString(2) // this gives "1100100"

Tags: , , ,

Popularity: 100% [?]


1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.75 out of 5)
Loading ... Loading ...

Check out this video!!

Write a Comment

Take a moment to comment and tell me what you think. Some basic HTML is allowed for formatting.

*
To prove that you're not a bot, enter this code
Anti-Spam Image

Type your comment here.

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Reader Comments

Be the first to leave a comment!