Home > Java > javaTutorial > body text

Java freemarker retains two decimal places sample code sharing

黄舟
Release: 2017-03-21 10:55:49
Original
2258 people have browsed it

This article mainly introduces the implementation method of Java and freemarker to retain two decimal places, and then gives you a detailed explanation of freemarker retaining two decimal places. Friends in need can refer to it

1 , Java retains 2 decimal places

double acc = 22.4322;
String accX = String.format("%.2f", acc);
Copy after login

2. Freemarker retains two decimal places

<#if centerFreeSize??>
  ${centerFreeSize?string("#.##")}
<#else>
  0.00
</#if>
Copy after login

Supplement: freemarker retains decimal places

freemarker retains two decimal places

Method 1

#{number ;m1M2} m Minimum number of digits Decimal, the maximum number of decimal places in M

Method 2

${x?string("0.##")}

It is easy to overlook one point in development. If you input a value (maybe a decimal) and do not process it when outputting, it is easy to cause

invisible BUG. For example, if a value of 0.22 is retrieved from the database, ${x?if_exists?html} is generally output. In this case, 0 is displayed instead of 0.22.

should be written as ${x?if_exists?string.number} or ${x?if_exists.toString()?html}

1, numbers less than 1 are subtracted incorrectly in freemarker

2. The format of numbers such as 1000 and 2000 in freemarker is wrong and will be interpreted as 1,000

Solution Set the display format of number in freemarker

XmlCode

<#setting number_format="0.00"> 
  <#assign x="${goodsgroupt.memberprice}"/> 
  <#assign y="${goodsgroupt.groupprice}"/> 
  ${(x?number-y?number)}
Copy after login

The key point is this number_format

It is said online that number_format="0" can only solve the second situation.

4. Local settings

Features:

If you are on a certain page, or several pages (other pages can be imported), use The formatting of the numbers is the same. You can consider configuring the formatting form of the numbers in a unified manner.

Usage:

Just set it before using the numbers that need to be formatted. The format is as follows : <#setting number_format=",##0.##"> The content of ",##0.##" adopts a syntax similar to the number format in Java, which is the third point above.

Note:

If used on a page, the entire page will be in that format by default, unless the default format is overridden by the string function. Similarly, if it is placed on a public page, As long as other pages include it, they will have the same format.

5. Global settings

Features:

Number formatting forms are provided by default on all pages

Usage

(for spring): Set the default number formatting form in the freemarker configuration file, as follows:

The code is as follows

< property name = "freemarkerSettings" >
     < props>
        .....
       < prop key = "number_format"> 0.## </prop >
        ......
     </props>
</property>
Copy after login

The above is the detailed content of Java freemarker retains two decimal places sample code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!