EzDevInfo.com

format interview questions

Top format frequently asked interview questions

How can I pretty-print JSON?

Is there a (unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

{ "foo": "lorem", "bar": "ipsum" }

... into something like this:

{
    "foo": "lorem",
    "bar": "ipsum"
}

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)

Advertisements

Difference between format specifiers %i and %d in printf

What is the difference between %d and %i when used as format specifiers in printf?


Source: (StackOverflow)

How can I print a literal "{}" characters in python string and also use .format on it?

x = " \{ Hello \} {0} "
print x.format(42)

gives me : Key Error: Hello\\

I want to print the output: {Hello} 42


Source: (StackOverflow)

How do I render a partial of a different format in Rails?

I'm trying to generate a JSON response that includes some HTML. Thus, I have /app/views/foo/bar.json.erb:

{
  someKey: 'some value',
  someHTML: "<%= h render(:partial => '/foo/baz') -%>"
}

I want it to render /app/views/foo/_baz.html.erb, but it will only render /app/views/foo/_baz.json.erb. Passing :format => 'html' doesn't help.


Source: (StackOverflow)

How to set date format in HTML date input tag?

I am wondering whether it is possible to set the date format in the html <input type="date"></input> tag... Currently it is yyyy-mm-dd, while I need it in the dd-mm-yyyy format.


Source: (StackOverflow)

Python TypeError: not enough arguments for format string

Here's the output. These are utf-8 strings I believe... some of these can be NoneType but it fails immediately, before ones like that...

instr = "'%s', '%s', '%d', '%s', '%s', '%s', '%s'" % softname, procversion, int(percent), exe, description, company, procurl

TypeError: not enough arguments for format string

Its 7 for 7 though?


Source: (StackOverflow)

Using printf with a non-null terminated string

Suppose you have a string which is NOT null terminated and you know its exact size, so how can you print that string with printf in C? I recall such method but I can not find out know...


Source: (StackOverflow)

Correct format specifier to print pointer (address)?

Which format specifier should I be using to print the address of a variable? I am confused between the below lot.

%u - unsigned integer

%x - hexadecimal value

%p - void pointer

Which would be the optimum format to print an address?


Source: (StackOverflow)

Formatting a number with leading zeros in PHP

I want have a variable which contains the value 1234567.

I want it to contain exactly 8 digits i.e. 01234567.

Is there a PHP function for that?


Source: (StackOverflow)

Android TextView Justify Text

How do you get the text of a TextView to be Justified (with text flush on the left- and right- hand sides)?

I found a possible solution here, but it does not work (even if you change vertical-center to center_vertical, etc).


Source: (StackOverflow)

How to auto-format code in Eclipse?

How do you auto-format code in Eclipse?


Source: (StackOverflow)

Convert one date format into another in PHP

Is there a simple way to convert one date format into another date format in PHP?

I have this:

$old_date = date('y-m-d-h-i-s');            // works

$middle = strtotime($old_date);             // returns bool(false)

$new_date = date('Y-m-d H:i:s', $middle);   // returns 1970-01-01 00:00:00

But I'd of course like it to return a current date rather than the crack 'o dawn. What am I doing wrong?


Source: (StackOverflow)

How to use java.String.format in Scala?

I am trying to use a .format method of a string. But if I place %1, %2, etc. in the string, java.util.UnknownFormatConversionException is thrown pointing to a confusing Java source code piece:

private void checkText(String s) {

    int idx;

    // If there are any '%' in the given string, we got a bad format
    // specifier.
    if ((idx = s.indexOf('%')) != -1) {
        char c = (idx > s.length() - 2 ? '%' : s.charAt(idx + 1));
        throw new UnknownFormatConversionException(String.valueOf(c));
    }
}

From this I understand that % char is forbidden. If so, then what should I use for argument placeholders?

I use Scala 2.8.


Source: (StackOverflow)

PHP Function Comments

Just a quick question: I've seen that some PHP functions are commented at the top, using a format that is unknown to me:

/**
 *
 * Convert an object to an array
 *
 * @param    object  $object The object to convert
 * @return      array
 *
 */

My IDE gives me a dropdown selection for the things such as @param and @return, so it must be documented somewhere. I've tried searching google but it won't include the @ symbol in its search.

What is this format of commenting and where can I find some information on it?


Source: (StackOverflow)