How to generate a number from the text in Javascript?
source link: https://frontendroom.com/generate-number-from-text/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to generate a number from the text in Javascript?
Generating a number from a text is an interesting problem.
In our day to day life as developers, we generally come to a problem where we want to calculate an equivalent number for a particular string.
Sometimes, we don't want absolute numbers but the equivalent number which might be distinguishable just.
If the text contains only numeric values in it, then it is very easy to convert it to a number in Javascript, and in fact, that equivalent number would be an absolute number.
IF TEXT CONTAINS NUMERIC VALUES:
If the text contains a numeric value only, then here is how we convert it to numbers.
Use + operator
By using plus (+) operator, we can convert it to numbers easily. You can find more information about the +
operator in this article → How do Unary Plus and Unary Negation operators behave in Javascript?.
Use parseInt method
By using the parseInt method, we can also convert the text to numbers.
IF TEXT CONTAINS NON NUMERIC VALUES:
If the text contains non-numeric values such as alphabets, special characters etc, then using the + operator
or parseInt
method will return NaN
as the value.
It is next to impossible to find the absolute value of non-numeric text but we can find the equivalent random number for it.
So if you have a bunch of texts and you just need to give them some unique set of numbers or random numbers, we can do that.
Generate a random number
In order to calculate an equivalent number from the text, we need to
- Calculate the hex of the text (using CRC algorithms )
- Convert that hex value to decimal
In order to find the equivalent hex value of a text, we will be using crc
npm package. We will use crc32
to convert text to the hex value.
Now using hexToDecimal
converter, we can calculate the equivalent number.
So this is how we calculate the equivalent random number for a particular non-numeric text.
Recommend
-
71
build-number-generator Generates a build number to be appended to your product version number, which is unique for each bui...
-
4
Be careful with random generate number This is the program I have used for a month: import numpy as np np.random.seed(202105) rand = np.random.rand() # business logic code using 'rand'
-
1
Swift- Generate Random Number · GitHub Instantly share code, notes, and snippets. // Generate a random number (Int, Double, Float..) let random1 =...
-
2
How To Generate A Valid Credit Card Number For A Bin (First 6 Digits) March 25, 2014 There is plenty of generators that can p...
-
11
How to Generate Random Number in PHP 248 views 6 months ago PHP When you need to save multiple files in same directory...
-
6
AI Can Now Generate Videos From Text, Courtesy of FacebookMake-A-Video is the first time we are seeing a text-to-video tool that will soon be available to the public. September 29, 2022, 5:19pm...
-
2
Now entering the third dimension — 3D for everyone? Nvidia’s Magic3D can generate 3D models from text New AI aims to democratize 3D content creation, no modeling skills req...
-
4
img2txt-rs A cli to generate text image. Usage It’s easy to use this tool generating text image, all you need to do is provide a path of a image. For example: $ img2txt ./examples/exa...
-
10
-
4
Ranked #19 for today
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK