This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 5 years ago.
I have a number, for example:
25297710.1088
I need to add a bit between them and leave two characters after the point.
For example:
25 297 710.10
While I stopped at this:
$(td).text().reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse());
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
From this code I get the following:
25 297 710.1 088
Where $(td).text() I get a number from the cell of the row in the table.
If I have numbers, for example:
25297710.10
then i get:
25 297 710.10
It's ok.
What I need to do to leave two characters after the point?
You can use a RegExp to format the number/string. The input is converted to string using the relevant toString method.
function formatNumber(input) {
return input.toString().replace(/\d*(\d{2})(\d{3})(\d{3})\.(\d{2})\d*$/, "$1 $2 $3.$4");
}
var str = "25297710.1088";
var num1 = 25297710.1088;
var num2 = 2545454545454.2254;
var num3 = 232545454511112.3354122313123123;
console.log(formatNumber(str));
console.log(formatNumber(num1));
console.log(formatNumber(num2));
console.log(formatNumber(num3));
I think you can do next steps:
1) you have 25 297 710.10
2) you find position of dot symbol ->#pos
3) you replace bits in string in range between #pos and end of your string
4) you cut string after dot to 2 characters
Related
This question already has answers here:
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
(10 answers)
Closed 5 years ago.
I know this has been asked a million times, but I just can't seem to crack it.
I have this:
function checkPassword(strPassword)
{
var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");
var blnResult = objPattern.test(strPassword);
return(blnResult)
}
...but it only seems to check the length, and not if there's a number?
What have I missed?
Edit:
The number can be anywhere in the string, not necessarily at the end.
Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable
If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$
You can just include both tests separately in your function:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
Demo:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
console.log(p+" ::: "+ checkPassword(p));
});
This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 8 years ago.
I want to convert an number (integer) to a hex string
2 (0x02) to "\x02"
or
62 (0x0062) to "\x62"
How can I do that correctly?
You can use the to string method:
a = 64;
a.toString(16); // prints "40" which is the hex value
a.toString(8); // prints "100" which is the octal value
a.toString(2); // prints "1000000" which is the binary value
Well, it's seems that you want just to concatenate the integer with \x.
If so just to like that:
var number = 62;
var hexStr = '\x' + number.toString(16);
But you have something strange about explaining.
Note: that 62 is not the same as 0x62, 0x62 would be 98.
var converted = "\x" + number.toString(16)
I need to validate a textbox field that will contain a range (separated by -). Following are the requirements:
Need to validate year & month ranges, and have values like 0.5 - 3.11 for denoting 5 months to 3 years and 11 months
The decimal places can be max 2 and 11 is max value in decimal place while 0 is minimum.
Both parts separated by hyphen -, may or may not include 1 blank space (only before and after hyphen).
The left part must always be less than right part.
Should validate values like:
1
2.3
2.3 - 4.6
2.3-4.6
2.4-2.1 is invalid
No negative required for the float values
I tried to generate some regex but the closest was:
(0|([1-9][0-9]{0,9}))(\.[0-9]{1,2})?(-)(0|([1-9][0-9]{0,9}))(\.[0-9]{1,2})?
but it can only validate values like 1.3-1.9 but does not compares the left and right part. And only a single digit value is also not validated.
You could do most of the validation with a regex, but checking that the first value is less than the second value would have to be done in code rather than a regex.
function isRangeStringValid(r) {
if (!r.match(/(0|[1-9]\d*)\.(\d|1[01]) ?- ?(0|[1-9]\d*)\.(\d|1[01])/)) {
return false;
}
var y1 = parseInt(RegExp.$1);
var m1 = parseInt(RegExp.$2);
var y2 = parseInt(RegExp.$3);
var m2 = parseInt(RegExp.$4);
return (y1<y2 || (y1==y2 && m1<m2));
}
yet another variant function with regexp for solution
function israngev(r){
var res = r.match(/^\d+$|^\d+\.(\d|1[01])$|^(\d+\.(\d|1[01])) ?- ?(\d+\.(\d|1[01]))$/);
return res!=null &&
(!(res[1]||res[2]||res[4]) ||
(res[1]!=null ||
(res[4]-res[2]>0)))}
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
I can't figure out how to solve the following problem.
I have an array of numbers from 1 to 100.
I need to convert them to strings but to a length of 5.
So, for instance:
1 becomes 00001
2 becomes 00002
3 becomes 00003
4 becomes 00004
etc, etc..
It seems so easy but I cannot find a function. The best I found was .toFixed(n) which is the number of decimal points to use.
Here's a very simple padding function:
function padLeft(str, length, paddingCharacter) {
str = '' + str; //Make sure that we convert it to a string if it isn't
while (str.length < length) {
str = paddingCharacter + str; //Pad it
}
return str;
}
padLeft(123, 5, '0'); //00123
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to print number with commas as thousands separators in Javascript
I have a function that will add thousand seperators to a number, however it is not working well when a decimal is passed in:
function thousandSep(val) {
return String(val).split("").reverse().join("")
.replace(/(.{3}\B)/g, "$1,")
.split("").reverse().join("");
}
If I pass in 10000, I get 10,000 as expected.
However, passing in 10,000.00 I get 1,000,0.00.
How can I modify the function to handle decimals?
Don't use ., use \d
function thousandSep(val) {
return String(val).split("").reverse().join("")
.replace(/(\d{3}\B)/g, "$1,")
.split("").reverse().join("");
}
function format(n, sep, decimals) {
sep = sep || "."; // Default to period as decimal separator
decimals = decimals || 2; // Default to 2 decimals
return n.toLocaleString().split(sep)[0]
+ sep
+ n.toFixed(decimals).split(sep)[1];
}
format(4567354.677623); // 4,567,354.68