public class Main { public static void main(String[] args) { // printf() = an optional method to control, format, and display text to the console window // two arguments = format string + (object/variable/value) // % [flags] [precision] [width] [conversion-character]
//[width] // minimum number of characters to be written as output //System.out.printf("Hello %10s",myString);
//[precision] // sets number of digits of precision when outputting floating-point values //System.out.printf("You have this much money %.1f",myDouble);
// [flags] // adds an effect to output based on the flag added to format specifier // - : left-justify // + : output a plus ( + ) or minus ( - ) sign for a numeric value // 0 : numeric values are zero-padded // , : comma grouping separator if numbers > 1000
//System.out.printf("You have this much money %,f",myDouble); } }
First of all: Thank you so much for all your content, I'm sure it has helped countless people in their coding endeavours. Keep it up! :) Now for a question: why would the coversion-character for integers be named after "decimal", when those are, by definition at least, the "opposite" of integers? :D It doesn't take away from the lesson in the least, but it bugs me and perhaps you, or someone else, find the time to explain or just agree that it doesn't make sense. PS: I think you mixed up [precision] and [width] in the order, at least it worked this way when i experimented with the formatting :D // % [flags] [width] [precision] [conversion-character]
I think I know all of your java videos lol. Thanks a lot. A note I wanted to add for other viewers: printf does not automatically go to the next line, similar to System.out.print(); for that, include a " " in the printf statement like this: Example: double aDouble = 5.5555; System.out.printf("%.1f ", aDouble); this prints the value "5.5" and goes to the next line just like System.out.println(5.5); would do. I state this because if you printf twice in a row with double or float values and do not include the " " then you may go crazy looking for the error because you will get something like this: Example: double a = 5.1211 double b = 4.4511 System.out.printf("%.2f", a); System.out.printf("%.2f", b); The output would be: 5.124.45 since there is no " " included, 4.45 will simply be appended to 5.12 -> 5.124.45 adding " " prevents this from happening: System.out.printf("%.2f ", a); System.out.printf("%.2f ", b); output: 5.12 4.45 Cheers
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
Why does he not explain the "%020f" part for when you do a lower number than 20? For me it started with 3 zeros at "%015f" then added zeros up to 8 at 20. I don't know if it's really that important in the grand scheme of things, but it definitely through me for a loop trying to understand that part.
Whenever I write printf and print int with a phrase using %d it doesn't work. It works only with char and String, not with number: double, float, int. Can anyone tell me what to do?
% [flags] [width] [precision] [conversion-character] i think that is the right sequence because I had the same question. So: double myDouble = 1000; System.out.printf("%,20.2f",myDouble); //that should do the trick
so this just makes it so i dont have to create and import DecimalFormat DF = new DecimalFormat for double outputs, i should just use printf for any formatting?
When I Watch his videos I Always Think , When I'll be An Software Engineer , I'll Give 50% Credit to BRO 'Cause He helped me a Lot . Much Better than my Damn school.
public class Main {
public static void main(String[] args) {
// printf() = an optional method to control, format, and display text to the console window
// two arguments = format string + (object/variable/value)
// % [flags] [precision] [width] [conversion-character]
boolean myBoolean = true;
char myChar = '@';
String myString = "Bro";
int myInt = 50;
double myDouble = 1000;
// [conversion-character]
//System.out.printf("%b",myBoolean);
//System.out.printf("%c",myChar);
//System.out.printf("%s",myString);
//System.out.printf("%d",myInt);
//System.out.printf("%f",myDouble);
//[width]
// minimum number of characters to be written as output
//System.out.printf("Hello %10s",myString);
//[precision]
// sets number of digits of precision when outputting floating-point values
//System.out.printf("You have this much money %.1f",myDouble);
// [flags]
// adds an effect to output based on the flag added to format specifier
// - : left-justify
// + : output a plus ( + ) or minus ( - ) sign for a numeric value
// 0 : numeric values are zero-padded
// , : comma grouping separator if numbers > 1000
//System.out.printf("You have this much money %,f",myDouble);
}
}
Well explained bro thanks am going to smash my test today
First of all: Thank you so much for all your content, I'm sure it has helped countless people in their coding endeavours. Keep it up! :)
Now for a question: why would the coversion-character for integers be named after "decimal", when those are, by definition at least, the "opposite" of integers? :D
It doesn't take away from the lesson in the least, but it bugs me and perhaps you, or someone else, find the time to explain or just agree that it doesn't make sense.
PS: I think you mixed up [precision] and [width] in the order, at least it worked this way when i experimented with the formatting :D
// % [flags] [width] [precision] [conversion-character]
This taught me printf better in 11 minutes than my college professor or textbook did in 3 months. Thank you
Dude same! Why do professors overly complicate java so much
@@nickgavrilis2079Learning I/O streams 😢
I think I know all of your java videos lol. Thanks a lot.
A note I wanted to add for other viewers:
printf does not automatically go to the next line, similar to System.out.print();
for that, include a "
" in the printf statement like this:
Example:
double aDouble = 5.5555;
System.out.printf("%.1f
", aDouble);
this prints the value "5.5" and goes to the next line just like System.out.println(5.5); would do.
I state this because if you printf twice in a row with double or float values and do not include the "
" then you may go crazy looking for the error because you will get something like this:
Example:
double a = 5.1211
double b = 4.4511
System.out.printf("%.2f", a);
System.out.printf("%.2f", b);
The output would be:
5.124.45
since there is no "
" included, 4.45 will simply be appended to 5.12 -> 5.124.45
adding "
" prevents this from happening:
System.out.printf("%.2f
", a);
System.out.printf("%.2f
", b);
output:
5.12
4.45
Cheers
thx bro
useful to know arigato :3
Forward slash
I can't imagine myself learning this all alone. you are making the work smoother for me. good effort bro, keep it up!
For me also BRO
This video helped me understand the printf() method better. Thank you. Keep up the good work !!
you're even better than any of my professors at college.
I couldnt learn this from my professor in 1 month but i can learn from bro code in 11 minutes!
THANKS A LOT!!!
Thanks brocode, you are going to be helpful to a lot of software engineers
I love the way you explain everything it's quick precise and clear.... I love it!
tbh didnt need this info rn, but after watching a few of your vids your explanations are very clear and precise! thats a sub for me
System.out.println(" 100% Better than paid online classes");
System.out.printf("%d Better than Paid Online Classes!", 100);
boolean thisStatement = true;
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
you are one of the best java teacher
wonderful explanation💛💛💛💛
great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much
Good Turotial
Great video
Very nice sir
great work bro
nicely explained
Amazing video, so easy to follow, goated bro.
i really appreciate your assistance brother .God bless you
This would have helped me earlier this week! Need to watch more videos.
Thank for the amazing video
Considering how much headache you have saved us from, you are definitely going to heaven.
your videos are very helpfull
Nice
can't believe such good resources are available for free
Bro, I'm very appreciative of your help.
awesome teaching brother :)
Thanks for the great effort.
nice
dude i was having such a hard time understanding thank you so much !
Awesome
good topic. i had no idea, printf exists in java too
Thanks sir
Yow bro, thank for this tutorials men. It means a lot to me I learned a lot from your videos men, KEEP IT UP BRO!!
very godd videos sir open your branch in india
best bro
Thanks, Bro! ☕ You're awesome!
Great explanation! Keep it up!
NIce.
Osm Bro 🔥🔥🔥 Thank U
I just really want to thank you😭! I needed to make my float to only have 4 decimals
Thank you man, very clear video.
Thanks King
Damn! You are soo good :0
Thanks so much man, really helped me
Thanks
thank you very much
This is so very helpful! Thank you so much!
Honestly, better than my teacher
thanks bro
Thanks so much bro
weirdly enough, in my courses' labs we needed formatting yet we've never been taught it
i was looking exactly for this
i thought id have to use your c tutorial
Thank bro
thank you, i was struggling earlier
Great video!
Bro is a pro
Thank you so much ❤
You saved my day
Bro Code really do be feeling like my Bro for code
bro? 😳
thanks
Thanks, Bro!
Why does he not explain the "%020f" part for when you do a lower number than 20? For me it started with 3 zeros at "%015f" then added zeros up to 8 at 20. I don't know if it's really that important in the grand scheme of things, but it definitely through me for a loop trying to understand that part.
thank you bro :)
Whenever I write printf and print int with a phrase using %d it doesn't work.
It works only with char and String, not with number: double, float, int.
Can anyone tell me what to do?
Showing example code would be helpful
Thank you sir.
love u bro
thank you so much!!
thx 4 vid bro !
thank you,.
thx bro
Thank you.
thanks bro!
So… kind of like a f-string in python. Nice Bro!
Thank you!
Bro
You are doing an amazing job Bro 😍
Thank you, Bro
I wonder how do I do if I want to add .2 as precision and 20 in width in the same time. It doesn't work when I tried it.
% [flags] [width] [precision] [conversion-character]
i think that is the right sequence because I had the same question. So:
double myDouble = 1000;
System.out.printf("%,20.2f",myDouble);
//that should do the trick
why didnt i know about printf sooner?
i thought only print and println existed in java😭
thanks a lot!!!
so this just makes it so i dont have to create and import DecimalFormat DF = new DecimalFormat for double outputs, i should just use printf for any formatting?
gg bang
Is there a way to set the [width] in the string formatting to a variable that can be input from the user?
ya !
thanks bro🤍
was this for matt?
When I Watch his videos I Always Think , When I'll be An Software Engineer , I'll Give 50% Credit to BRO 'Cause He helped me a Lot .
Much Better than my Damn school.
Please don’t capitalize every word in your sentence. I had a stroke reading that
How do you put a zero in front of an integer?
Thanks, Bro 06/03/2024
Thanks!!
Η συγκεκριμένη μέθοδος printf() υπάρχει και στη γλώσσα προγραμματισμού C.
Hi from Ukraine!! U cool man
thanks Maksi!
This printf() is from C language with the %d, %f ans %s.
woooo code
W
some food for the allmighty algorithm : "great , concise, high quality, .addrandomAdjective(); "
😀
Can i ask why it's not working on me?
formatting method