Java में Numbers Print करना
In this page:
Printing Numeric Literals
System.out.println numeric literals सीधे print कर सकता है, बिना किसी conversion के, और number को बिल्कुल जैसा लिखा गया वैसा दिखाएगा, चाहे यह एक whole integer हो या एक decimal value।
उदाहरण: Printing Numeric Literals
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(42);
// Print a line to the console
System.out.println(3.14);
}
}
Login to try C/C++/Java/PHP code in the editor
Printing Arithmetic Results
जब println को एक mathematical expression दिया जाता है, Java पहले उस expression को पूरी तरह evaluate करता है और सिर्फ तब single resulting number print करता है, original expression text नहीं।
उदाहरण: Printing Arithmetic Results
public class Main {
public static void main(String[] args) {
System.out.println(5 + 3); // evaluated first - prints 8, not "5 + 3"
}
}
Login to try C/C++/Java/PHP code in the editor
Printing Different Number Types
println Java के हर numeric type के साथ काम करता है -- byte, short, int, long, float, और double -- और हर एक को उस type के अपने conventions के अनुसार formatted print करता है, जैसे printed output में कभी L suffix न दिखना।
उदाहरण: Printing Different Number Types
public class Main {
public static void main(String[] args) {
byte b = 10;
long l = 100L;
double d = 2.5;
System.out.println(b);
System.out.println(l); // no 'L' suffix in the printed output
System.out.println(d);
}
}
Login to try C/C++/Java/PHP code in the editor
Concatenating Numbers with Text
जब एक number को + operator उपयोग करके एक String के साथ combine किया जाता है, Java concatenate करने से पहले अपने आप number को इसके String representation में convert करता है, यही कारण है कि numbers printed text में सीधे mix किए जा सकते हैं।
उदाहरण: Concatenating Numbers with Text
public class Main {
public static void main(String[] args) {
int age = 25;
System.out.println("Age: " + age); // age auto-converted to a String
}
}
Login to try C/C++/Java/PHP code in the editor
Formatting Numbers in Output
किसी number के दिखने पर ज़्यादा control के लिए, printf और String.format decimal places की एक fixed संख्या के लिए %.2f या बड़े integers में thousands separators insert करने के लिए %,d जैसे format specifiers accept करते हैं।
उदाहरण: Formatting Numbers in Output
public class Main {
public static void main(String[] args) {
System.out.printf("%.2f%n", 3.14159); // 2 decimal places
System.out.printf("%,d%n", 1000000); // thousands separators
}
}
Login to try C/C++/Java/PHP code in the editor
"Sum: " + 1 + 2लिखना और3की उम्मीद करना, जब यहSum: 12print करता है;"Sum: " + (1 + 2)उपयोग करें।System.out.println(5 / 2)से2.5print होने की उम्मीद करना, जब integer division2print करता है।- एक float literal पर
fभूल जाना, इसलिए value एकdoubleहै और ज़्यादा digits के साथ print होती है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: