Java freemarker retains two decimal places sample code sharing
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);
2. Freemarker retains two decimal places
<#if centerFreeSize??> ${centerFreeSize?string("#.##")} <#else> 0.00 </#if>
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)}
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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
