I'm trying to use JSTL to build a form. I have a select input for the months but I need the months to always be two digits i.e. padded left with a Zero for 1-9.
I have this but obvious it doesn't give me what I want.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<select class="formInput">
<c:forEach var="i" begin="1" end="12" step="1" varStatus ="status">
<option><fmt:formatNumber pattern="##" value="${i}" /></option>
</c:forEach>
</select>
This has to have been done before but I can't find an example after a bit of searching.
Step 1: Get the Number N and number of leading zeros P. Step 2: Convert the number to string using the ToString () method. Step 3: Then pad the string by using the PadLeft () m ethod.
Like MySQL, MariaDB also has an LPAD () function that allows us to pad the left part of a string or number with our chosen character or series of characters: And like with MySQL, we can pass the number as a numeric type, so there’s no need to convert it to a string first. See How to Pad a Number with Leading Zeros in MariaDB for more.
You can add leading zeros to both integer and floating-point numbers by using a custom numeric format string. This article shows how to use both methods to pad a number with leading zeros. Determine the minimum number of digits you want the integer value to display.
Like Oracle, PostgreSQL also has an LPAD () function that enables us to pad numbers with leading zeros. See 2 Ways to Add Leading Zeros in PostgreSQL for an example.
found the answer: minIntegerDigits
<select class="formInput">
<c:forEach var="i" begin="1" end="12" step="1" varStatus ="status">
<option><fmt:formatNumber minIntegerDigits="2" value="${i}" /></option>
</c:forEach>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With