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.
Related
This question already has answers here:
How do you convert numbers between different bases in JavaScript?
(21 answers)
Closed 5 months ago.
I have some numbers (base 10) that I want to convert to a 32 bits(base 2), I have tried a lot of things, I found out the >>> operator, but apparently it only converts negative numbers to a base 10 the equivalent of the 32 bits, instead of base 2
const number = 3
const bitNumber = 1 >>> 0
console.log(bitNumber) /// 1
The numbers are always stored as bits internally. It’s console.log that converts them to a string, and the string conversion uses decimal by default.
You can pass a base to Number.prototype.toString:
console.log(bitNumber.toString(2));
and display as many bit positions as you want:
console.log(bitNumber.toString(2).padStart(32, '0'));
This question already has answers here:
Generate big numbers with Math random in Javascript
(4 answers)
Closed 1 year ago.
I'm using this one liner to generate a random 18 digit number :
Math.floor(100000000000000000 + Math.random() * 900000000000000000)
The problem is that it always contains one or more zeros at the end, and I don't understand why.
How can I generate a really random number that always contains 18 digits, and without a leading zero?
You have too many digits, you're running against the limit to precision in a javascript number.
It looks like a Problem with the max number Range. You can hack it like this. And it is a better Random number.
Math.floor(10000000000000000 + Math.random() * 90000000000000000)+ "" + Math.floor(Math.random()* 100)
But if you parse to int you have the same problem.
The solution for this so far is to generate two numbers (10 digits + 8 digits) & concatenate them as follow:
Math.floor(1000000000 + Math.random() * 9000000000) + "" + Math.floor(10000000 + Math.random() * 90000000)
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 1 year ago.
In my project, when I am subtracting 96.74 from 60, javascript is giving me 36.739999999999995 , but I need 36.74 to do further calculation.
What should I do, how do I overcome this problem??
Thanks in advance.
Use parseFloat and set the decimal places to 2 with .toFixed(2)
console.log(parseFloat(36.739999999999995).toFixed(2))
If you want to get rid of trailing zeroes cast the result to a Number:
var num = Number(parseFloat(36.7).toFixed(2));
console.log(num);
Example how to round to two decimals, if that was what you wanted?
var x = 36.739999999999995;
x = Math.round(x*Math.pow(10,2))/Math.pow(10,2);
console.log(x);
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());
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;