EzDevInfo.com

numbers.js

Advanced Mathematics Library for Node.js and JavaScript

Limits of Nat type in Shapeless

In shapeless, the Nat type represents a way to encode natural numbers at a type level. This is used for example for fixed size lists. You can even do calculations on type level, e.g. append a list of N elements to a list of K elements and get back a list that is known at compile time to have N+K elements.

Is this representation capable of representing large numbers, e.g. 1000000 or 253, or will this cause the Scala compiler to give up?


Source: (StackOverflow)

How to convert floats to human-readable fractions?

Let's say we have 0.33, we need to output "1/3".
If we have "0.4", we need to output "2/5".

The idea is to make it human-readable to make the user understand "x parts out of y" as a better way of understanding data.

I know that percentages is a good substitute but I was wondering if there was a simple way to do this?


Source: (StackOverflow)

Advertisements

How to check that a number is float or integer?

How to find that a number is float or integer?

1.25 --> float  
1 --> integer  
0 --> integer  
0.25 --> float

Source: (StackOverflow)

Can I hide the HTML5 number input’s spin box?

Is there a consistent way across browsers to hide the new spin boxes that some browsers (such as Chrome) render for HTML input of type number? I am looking for a CSS or JavaScript method to prevent the up/down arrows from appearing.

<input id="test" type="number"/>

Source: (StackOverflow)

Get a random number focused on center

Is it possible to get a random number between 1-100 and keep the results mainly within the 40-60 range? I mean, it will go out of that range rarely, but I want it to be mainly within that range... Is it possible with JavaScript/jQuery?

Right now I'm just using the basic Math.random() * 100 + 1.


Source: (StackOverflow)

How to sort an array of integers correctly

Trying to get the highest and lowest value from a array that I know will contain only integers seems to be harder than I thought?

var numArray = [140000, 104, 99];
numArray = numArray.sort();
alert(numArray[0] + ", " + numArray[numArray.length - 1]);

I'd expect this to show "99, 140000". Instead it shows "104, 99". So it seems the sort is handling the values as strings?

Any way to get the sort function to actually sort on integer value?


Source: (StackOverflow)

How to zero pad a sequence of integers in bash so that all have the same width?

I need to loop some values,

for i in $(seq $first $last)
do
    does something here
done

For $first and $last, i need it to be of fixed length 5. So if the input is 1, i need to add zeros in front such that it becomes 00001. It loops till 99999 for example, but the length has to be 5.

E.g.: 00002, 00042, 00212, 012312 and so forth.

Any idea on how i can do that?


Source: (StackOverflow)

Why does 00.0 cause a syntax error?

This is weird. This is what happens at the JavaScript console in Chrome (version 42.0.2311.135, 64-bit).

> 0
< 0
> 00
< 0
> 0.0
< 0
> 00.0
X Uncaught > SyntaxError: Unexpected number

Firefox 37.0.2 does the same, although its error message is:

SyntaxError: missing ; before statement

There's probably some technical explanation regarding the way JavaScript parses numbers, and perhaps it can only happen when tinkering at the console prompt, but it still seems wrong.

Why does it do that?


Source: (StackOverflow)

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this?

Here is how I am doing it:

function numberWithCommas(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
    return x;
}

Is there a simpler or more elegant way to do it? It would be nice if it works with floats also, but that is not necessary. It does not need to be locale-specific to decide between periods and commas.


Source: (StackOverflow)

Java - Convert integer to string [duplicate]

Given a number:

int number = 1234;

Which would be the "best" way to convert this to a string:

String stringNumber = "1234";

I have tried searching (googling) for an answer but no many seemed "trustworthy".


Source: (StackOverflow)

Format Float to n decimal places

I need to format a float to "n"decimal places.

was trying to BigDecimal, but the return value is not correct...

public static float Redondear(float pNumero, int pCantidadDecimales) {
    // the function is call with the values Redondear(625.3f, 2)
    BigDecimal value = new BigDecimal(pNumero);
    value = value.setScale(pCantidadDecimales, RoundingMode.HALF_EVEN); // here the value is correct (625.30)
    return value.floatValue(); // but here the values is 625.3
}

I need to return a float value with the number of decimal places that I specify.

I need "Float" value return not "Double".

Thanks in advance.


Source: (StackOverflow)

How to add maxlength for HTML5 input type="number" element?

For input type="number" element, maxlength is not working. How can I restrict the maxlength for that number element?


Source: (StackOverflow)

JavaScript get number from string

I have a string in javascript like `#box2' and I just want the '2' from it.

Tried:

var thestring = $(this).attr('href');
var thenum = thestring.replace( /(^.+)(\w\d+\w)(.+$)/i,'$2');
alert(thenum);

It still returns #box2 in the alert, how can I get it to work?

It needs to accommodate for any length number attached on the end.


Source: (StackOverflow)

Algorithms based on number base systems? [closed]

I've noticed recently that there are a great many algorithms out there based in part or in whole on clever uses of numbers in creative bases. For example:

  • Binomial heaps are based on binary numbers, and the more complex skew binomial heaps are based on skew binary numbers.
  • Some algorithms for generating lexicographically ordered permutations are based on the factoradic number system.
  • Tries can be thought of as trees that look at one digit of the string at a time, for an appropriate base.
  • Huffman encoding trees are designed to have each edge in the tree encode a zero or one in some binary representation.
  • Fibonacci coding is used in Fibonacci search and to invert certain types of logarithms.

My question is: what other algorithms are out there that use a clever number system as a key step of their intuition or proof?. I'm thinking about putting together a talk on the subject, so the more examples I have to draw from, the better.


Source: (StackOverflow)

Get decimal portion of a number with JavaScript

I have float numbers like 3.2 and 1.6.

I need to separate the number into the integer and decimal part. For example, a value of 3.2 would be split into two numbers, i.e. 3 and 0.2

Getting the integer portion is easy:

n = Math.floor(n);

But I am having trouble getting the decimal portion. I have tried this:

remainer = n % 2; //obtem a parte decimal do rating

But it does not always work correctly.

The previous code has the following output:

n = 3.1 => remainer = 1.1

What I am missing here?


Source: (StackOverflow)