Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare int variable in JSP?

In JAVA I declared an int variable and need to print it. my code:

public int i=0;
System.out.println(i);

Output:1 but I my required output is 01 How can I do this?

Thanks in advance

like image 665
Mehedi Avatar asked Oct 20 '25 08:10

Mehedi


1 Answers

Do this :

<%
int var=1;
out.println(var<10 ? "0"+var : var);
%>

Or, more typically, isolating a little the algorithm from the presentation :

<%
int var=1;
%>
<%= var<10 ? "0"+var : var %>

If you want to do more complex number formating, have a look at DecimaFormat.

like image 93
Denys Séguret Avatar answered Oct 21 '25 21:10

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!