Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL padding int with leading zeros

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.

like image 713
kasdega Avatar asked Jul 20 '11 20:07

kasdega


People also ask

How to pad a string with leading zeros?

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.

How to pad a number with leading zeros in MariaDB?

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.

How do you add leading zeros to a floating point number?

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.

How to pad numbers with leading zeros in PostgreSQL?

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.


1 Answers

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>
like image 60
kasdega Avatar answered Sep 28 '22 08:09

kasdega