Float retains two decimal places

Source: Internet
Author: User

1. Page Operation formatting numbers

On the page sometimes use the number of operations, after the operation will appear 1.5999999999999 such a long number, need to format numbers, such as the retention of two valid digits

First import this label

<% @ taglib URI = " http://java.sun.com/jsp/jstl/fmt " prefix = " FMT " %>

Then use the label language

<type= "number"  value= "${cost-beannums}"  pattern = "0.00" maxfractiondigits = "2" />

2. Pass the value to the page before formatting the number

DecimalFormat decimalformat=New DecimalFormat ("#0.00"); // the character format of the constructed method here if the decimal place is less than 2 bits, it will be 0. String P=decimalformat.format (cost); // format returns a string

This time the value to the page is a string form, if the use of El expression, the following is wrong, because cost is a string form, can not compare size

<C:whenTest= "${beannums! = 0 && cost>beannums}"><spanclass= "Ml50 Pay_by_cash_span">¥<Fmt:formatnumbertype= "Number"value= "${cost-beannums}"pattern= "0.00"maxfractiondigits= "2"/></span></C:when>

So we need to do the math first.

<C:whenTest= "${beannums! = 0 && cost-beannums>0}"><spanclass= "Ml50 Pay_by_cash_span">¥<Fmt:formatnumbertype= "Number"value= "${cost-beannums}"pattern= "0.00"maxfractiondigits= "2"/></span></C:when>

Float retains two decimal places

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.