# Tag: math

## How do I get the quotient as int and remainder as a floating point in JavaScript

On my calculator when I do 18/7 I get 2.5714285714285714285714285714286. From my super limited Math skills 2 is the quotient and .5714285714285714285714285714286 is the remainder. How can I model this in JavaScript? Thanks! Answer

## check if number string contains decimal?

After doing a sqrt() How can I be check to see if the result contains only whole numbers or not? I was thinking Regex to check for a decimal – if it contains a decimal, that means it didn’t root evenly into whole numbers. Which would be enough info for me. but this code isnt working… I bet there’s other

## Calculating the maximum value for a decimal using scale and precision

I am working on a JavaScript function that takes two values: precision of a decimal value & scale of a decimal value. This function should calculate the maximum value that can be stored in a decimal of that size. For example: a decimal with a precision of 5 and a scale of 3 would have a maximum value of 99.999.

## Make a number a percentage

What’s the best way to strip the “0.”XXX% off a number and make it a percentage? What happens if the number happens to be an int? Answer A percentage is just: No need for anything fancy:

## JavaScript Number Split into individual digits

I am trying to solve a math problem where I take a number e.g. 45, or 111 and then split the number into separate digits e.g. 4 5 or 1 1 1. I will then save each number to a var to run a method on. Does anyone know how to split a number into individual digitals? For example I

## Javascript Math Object Methods – negatives to zero

in Javascript I can’t seem to find a method to set negatives to zero? -90 becomes 0 -45 becomes 0 0 becomes 0 90 becomes 90 Is there anything like that? I have just rounded numbers. Answer Just do something like or or

## JavaScript % (modulo) gives a negative result for negative numbers

According to Google Calculator (-13) % 64 is 51. According to Javascript (see this JSBin) it is -13. How do I fix this? Answer Taken from this article: The JavaScript Modulo Bug

## Write a truly inclusive random method for javascript

Javascript’s MATH object has a random method that returns from the set [0,1) 0 inclusive, 1 exclusive. Is there a way to return a truly random method that includes 1. e.g. While this always returns a number from the set [0,1] it is not truly random. Answer To put it bluntly, what you’re trying to do doesn’t make sense. Remember

## What is JavaScript’s highest integer value that a number can go to without losing precision?

Is this defined by the language? Is there a defined maximum? Is it different in different browsers? Answer JavaScript has two number types: Number and BigInt. The most frequently-used number type, Number, is a 64-bit floating point IEEE 754 number. The largest exact integral value of this type is Number.MAX_SAFE_INTEGER, which is: 253-1, or +/- 9,007,199,254,740,991, or nine quadrillion seven