To solve the problem Thymeleaf digital format rather think of several scenarios

background:

Output data of type double spring rear end, the front end of the frame using thymeleaf, double data type format, the need to display the original value (such as the original entry 5, and 5.00 can not be displayed), and thus needs to be stored value (stored value of decimal type, wherein two decimal) to zero after the decimal point for processing, and not with its own format thymeleaf embodiment.

Ideas:

1. Try Thymeleaf solution comes with its own function, failed (Thymeleaf method for processing itself can not be removed after the decimal point 0, it can only display a fixed number of decimal places).

2. The use of JS resolve, I feel some trouble, but also in combination with thymeleaf are more difficult to give up.

3. As Java background processing more adept at dealing with numerical formatting problems, and complete resolution tag thymeleaf also happened to work on the server side, then let thymeleaf framework can call Java methods? Theoretically possible, through trial and error, and finally experimental results.

4. Extension tab thymeleaf

After comparison, to achieve a simpler method 3, Specifically, the following:

Implementation:

Method 1: thymeleaf framework for Java static method call:

Write a Java class, which is written a static method for processing double format type data string is returned. Detailed code is as follows:

package com.hw.ibweb.util;

import org.apache.http.util.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by clyan on 2019/10/23 10:08.
 */
public class FormatUtil {

    /**
     * double小数点格式化
     * @param value
     * @return
     */
    public static String valueFormat(double value) {
        DecimalFormat df = new DecimalFormat("#####.##");
        String xs = df.format(new BigDecimal(value));
        return xs;
    }
}

Then, in the related html, the use of the following lines:

 <input type="text" class="form-control" width="250px" maxlength="6"
                                                           placeholder="最长6位数字"
                                                           name="code" th:attr="id=${item.dcitCode}"
                                                           th:value="${T(com.hw.ibweb.util.FormatUtil).valueFormat(item.money)}"
                                                           aria-label="Text input with checkbox"/>

That is, using the frame thymeleaf $ {T (fullclasspath) .function ()} interface solution, the following results:

So far, thymeleaf framework calls Java static method of thinking has been achieved, then thymeleaf framework can invoke Java instance methods? After practice we found that is not the problem.

Method Two: thymeleaf call Java instance method:

Examples of methods:

package com.hw.ibweb.util;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by clyan on 2019/10/10 11:57.
 */

public class bbb {

    public String valueFormat(double value) {
        DecimalFormat df = new DecimalFormat("#####.##");
        String xs = df.format(new BigDecimal(value));
        return xs;
    }
}

In html page, call the method worded as follows:

 <input type="text" class="form-control" width="250px" maxlength="6"
                                                           placeholder="最长6位数字"
                                                           name="code" th:attr="id=${item.dcitCode}"
                                                           th:value="${new com.hw.ibweb.util.bbb().valueFormat(item.money)}"
                                                           aria-label="Text input with checkbox"/>

Eventually effect as expected:

At this point, complete solution for double value formatting issues.

 

Guess you like

Origin www.cnblogs.com/jizhong/p/11725776.html