toString(16) with leading zeroes [duplicate] - javascript

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
When you convert a small number to a hexadecimal representation, you need leading zeroes, because toString(16) will return f for 15, instead of 00000f. Usually I will use the loop like this:
var s = X.toString(16); while (s.length < 6) s = '0' + s
is there a better way in JavaScript?
UPD: The answer suggested answer How can I create a Zerofilled value using JavaScript? is not what I am looking for, I look for a very short code, that is suited specifically for 24 bit integers converted to a hexadecimal looking strings.

How about
('00000'+(15).toString(16)).substr(-5)

Maybe this:
var s = X.toString(16);
s = "000000".substr(0, 6 - s.length) + s;

Related

Extracting float number from string using javascript [duplicate]

This question already has answers here:
how to extract floating numbers from strings in javascript
(3 answers)
Closed 6 years ago.
I have a string in javascript:
ECTS: 7.5 pts
From this string I need to extract 7.5 and parse it to a float value..
I've tried searching, but none of the solutions I found did the trick. I've tried:
var regex = /^\d+\.\d{0,3}$/;
var string = "ECTS: 7.5 pts";
var number = parseFloat(string.match(regex));
console.log("number is: " + number);
I've tried with a couple of different regular expressions, but none of them have done the trick.
I've created this fiddle to illustrate the problem.
EDIT1
I used to have this /\d+/ as my regex, but that didn't include floating point numbers.
EDIT2
I ended up using this as my regex /[+-]?\d+(\.\d+)?/g
Updated fiddle
This works nicely, no need for regex:
var s = "ECTS: 7.5 pts";
var n = parseFloat(s.split(" ")[1]);
$("#test").text(n);
did you try this?
var digits = Number((/(\d+\.\d+)/.exec('ECTS: 7.5 pts') || []).pop());

how to evaluate this Number("2/4") in JavaScript [duplicate]

This question already has answers here:
Evaluating a string as a mathematical expression in JavaScript
(26 answers)
Closed 7 years ago.
Hey guys i want to extract/evaluate the answer 2/4 in a string even ehen doing Number ("2/4") it gives me NaN as a result which is fairly reasonable! So my question is how can i evaluate this fraction from a string?
You can do eval("2/4"), which will properly result in 0.5.
However, using eval is a really bad idea...
If you always have a fraction in format A/B, you can split it up and compute:
var s = "11/47";
var ssplit = s.split('/');
document.body.innerText = ssplit[0] / ssplit[1];
Note that Division operator / will implicitly cast strings "11" and "47" to 11 and 47 Numbers.
You are looking for eval. Note
parseFloat("2/4")
2
parseFloat("4/2")
4
eval("4/2")
2
eval("2/4")
0.5
function myFunction() {
var str = "3/4";
var res = str.split("/");
alert(parseFloat(res[0]/res[1]));
}
Try with eval function :
eval("2/4");
Parsing the string only valid for numbers like 0-10 and a decimal (.) and all other if included will then result in NaN.
So, what you can do is like this:
Number(2/4)//0.5
parseFloat(2/4)//0.5
Number('2')/Number('4');//0.5
parseFloat('2')/parseFloat('4');//0.5
Number('2/4');//NaN as / is not parsable string for number
parseFloat('2/4');//2 as upto valid parsable string
parseFloat('1234/4');//1234
So, you can split string then use that like #Yeldar Kurmangaliyev answered for you.
(function(str){
var numbers = str.split("/").map(Number);
return numbers[0] / numbers[1];
})("2/4")
Keep in mind this does not check for invalid input.

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);

how to convert a decimal number to fraction using jquery [duplicate]

This question already has answers here:
Convert a decimal number to a fraction / rational number
(12 answers)
Closed 9 years ago.
I want to convert the decimal number 5.5 to 5 1/2. How can i do that?
I need to check for the number. If it is 5.5 then convert it to 5 1/2.
Please advice.
Using the fraction library and the vanilla framework instead of jQuery, i suppose something like this would work:
https://github.com/ekg/fraction.js
var num = 5.5
, rounded = Math.floor(num)
, rest = num - Math.floor(num);
var f = new Fraction(1, rest);
console.log(rounded + ' ' + f.numerator + '/' + f.denominator);
example: http://jsfiddle.net/QwTPY/
Try https://github.com/ekg/fraction.js or maths.js. Fraction.js was built for the purpose of working with fractions.

javascript parseInt [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parseInt a string with leading 0
If I parseInt("01") in javascript its not the same as parseInt("1")???
start = getStartEugene("MN01");
start2 = getStartEugene("MN1");
getStartEugene: function(spot) //ex: GT01 GT1
{
var yard = spot.match(/[0-9]+/);
var yardCheck = parseInt(yard);
if (yardCheck < 10)
return "this"+yard;
else
return "this0"+yard
}
I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09
But i am not getting it. Anyone know why?
You need to add the radix (2nd) argument to specify you are using a base 10 number system...
parseInt("01", 10); // 1
This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as #jondavidjohn correctly pointed).
parseInt("10"); // returns 10
parseInt("010"); // returns 8

Categories