EzDevInfo.com

numbers interview questions

Top numbers frequently asked interview questions

What's the best way to convert a number to a string in JavaScript?

What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ?

Some examples:

  1. String(n)

  2. n.toString()

  3. ""+n

  4. n+""


Source: (StackOverflow)

Haskell Int and Integer

In Haskell, what is the difference between an Int and an Integer? Where is the answer documented?


Source: (StackOverflow)

Advertisements

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)

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)

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)

PHP: show a number to 2 decimal places

What's the correct way to round a PHP string to 2 decimal places?

$number = "520"; // It's a string from a DB

$formatted_number = round_to_2dp($number);

echo $formatted_number;

Output is "520.00";

What's round_to_2dp look like?


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)

PHP prepend leading zero before single digit number, on-the-fly

PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?

Example:

$year = 11;
$month = 4;

$stamp = $year.add_single_zero_if_needed($month);  // Imaginary function

echo $stamp; // 1104

Source: (StackOverflow)

Check if a number has a decimal place/is a whole number

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (is an integer). For instance,

23 -> OK
5 -> OK
3.5 -> not OK
34.345 -> not OK
if(number is integer) {...}

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)

php test if number is odd or even

What is the simplest most basic way to find out if a number/variable is odd or even in PHP? Is it something to do with mod?

I've tried a few scripts but.. google isn't delivering at the moment.


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)

Python: Extract numbers from a string

I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method?

Example:

line = "hello 12 hi 89"

Result:

[12, 89]

Source: (StackOverflow)

How to limit the maximum value of a numeric field in a Django model?

Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e.g. 0.0-5.0 ?

Failing that, is there any way to restrict a PositiveIntegerField to only store, for instance, numbers up to 50?

Update: now that Bug 6845 has been closed, this StackOverflow question may be moot. - sampablokuper


Source: (StackOverflow)