filmov
tv
How To Format A String, integer, floating point number, character using the format() Method

Показать описание
How To Format A String, integer, floating point number, character using the format() Method
In this video, we will discuss on the Java String format() method, which we can use to format a String. Actually, we can use this method to perform many operations like for example joining Strings and formatting the output of the String that has been joined.
In this video we will have a look at some examples on how to use this method.
1. In the first example, I will show you how to use the format() method to convert values of certain types into Strings and then joining them
String name = “Brandon Cole”;
Since this method returns a String, we will declare a String variable
The output in the console will be: My name is Brandon Cole
As you can notice the Strings have been joined. The %s represents the argument position, that is used to pass the String variable name.
Here are the statements, if you want to pass other variable types
}
}
2. In the next example I want to show you how to play around formatting arguments
double height = 1.86;
Since this method returns a String, we will do a
The output in the console will be: My height is 1.8600
Notice that our decimal number format has changed because of what we specified in the argument by writing %.4f which means that the decimal number needs to be formatted to have four figures at its decimal part
Second example with Integer values
int number = 28;
The result in the console will show : 000028
The %06d formatted the integer value stored in variable number by adding zeros at the beginning of the integer value so that it will be made up of 6 figures
Another example with Strings
String str1 = "cool string";
String str2 = "28";
%1$ and %2$ are used for specifying argument positions.
%1$ is used for the first argument and refers to the first String str1
%2$ is used for the second argument and refers to the second String str2
#codingriver
#java
#programming
In this video, we will discuss on the Java String format() method, which we can use to format a String. Actually, we can use this method to perform many operations like for example joining Strings and formatting the output of the String that has been joined.
In this video we will have a look at some examples on how to use this method.
1. In the first example, I will show you how to use the format() method to convert values of certain types into Strings and then joining them
String name = “Brandon Cole”;
Since this method returns a String, we will declare a String variable
The output in the console will be: My name is Brandon Cole
As you can notice the Strings have been joined. The %s represents the argument position, that is used to pass the String variable name.
Here are the statements, if you want to pass other variable types
}
}
2. In the next example I want to show you how to play around formatting arguments
double height = 1.86;
Since this method returns a String, we will do a
The output in the console will be: My height is 1.8600
Notice that our decimal number format has changed because of what we specified in the argument by writing %.4f which means that the decimal number needs to be formatted to have four figures at its decimal part
Second example with Integer values
int number = 28;
The result in the console will show : 000028
The %06d formatted the integer value stored in variable number by adding zeros at the beginning of the integer value so that it will be made up of 6 figures
Another example with Strings
String str1 = "cool string";
String str2 = "28";
%1$ and %2$ are used for specifying argument positions.
%1$ is used for the first argument and refers to the first String str1
%2$ is used for the second argument and refers to the second String str2
#codingriver
#java
#programming