Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%s in String.format for numbers

Tags:

Is there any reason not to use %s for the format specifier when formatting numbers if no extra formatting is required?

e.g.

int answer = 42; String text = String.format("The answer is %s", answer); 

rather than the more usual:

int answer = 42; String text = String.format("The answer is %d", answer); 

The reason I'm asking is that I want to make some automated changes to source code, and it would be inconvenient to have to figure out whether the type of answer is int or String or something else

like image 782
Simon Nickerson Avatar asked Oct 24 '11 06:10

Simon Nickerson


People also ask

What is %s in string format?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following Python code illustrates the way of performing string formatting.

What does %s do in a string Java?

It means when % is encountered, the next character determines how to interpret the argument and insert it into the printed result. %s means interpret as string, %d is for digit, %x is digit in hexadecimal, %f is for float, etc.... It doesn't really 'do' anything, it's just a convention used for formatting text..

What is %s in coding?

the %s is a 'format character', indicating "insert a string here". The extra parameters after the string in your two function calls are the values to fill into the format character placeholders: In the first example, %s will be replaced with the contents of the command variable.

What is %s and %D in Java?

%s refers to a string data type, %f refers to a float data type, and %d refers to a double data type.


1 Answers

I would expect there to be a potential difference in terms of thousands separators and other locale-specific options. The first approach is just going to call Integer.toString (which is not locale-sensitive) because Integer doesn't implement Formattable. The second is going to use potentially locale-sensitive integer formatting - it could insert thousands separators, potentially even use a different set of digits etc.

Looking at the docs more carefully, it looks like grouping separators won't be applied without a specific formatting flag, but:

Number Localization Algorithm

After digits are obtained for the integer part, fractional part, and exponent (as appropriate for the data type), the following transformation is applied:

  • Each digit character d in the string is replaced by a locale-specific digit computed relative to the current locale's zero digit z; that is d - '0' + z.

Sample where it makes a difference:

import java.util.Locale;  public class Test {   public static void main(String[] args) {     Locale.setDefault(new Locale("hi", "IN"));     System.out.printf("String: %s; Number: %d\n", 1234, 1234);   } } 
like image 189
Jon Skeet Avatar answered Sep 22 '22 21:09

Jon Skeet