我在客戶端(javascript)中使用toFixed(2)。但在服務器端(Java)我必須重新計算。我怎樣才能做到這一點?Javascript to fixed fixed in java
5
A
回答
7
的BigDecimal
setScale()
會爲你做它在服務器端
import java.math.BigDecimal;
public class Main{
public static void main(String ar[]){
float number = 123.123456F;
BigDecimal numberBigDecimal = new BigDecimal(number);
System.out.println(numberBigDecimal);
numberBigDecimal = numberBigDecimal .setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(numberBigDecimal);
}
}
3
DecimalFormat的format方法是在java中相當於toFixed在JavaScript。
DecimalFormat decimalFormat = new DecimalFormat("0.##");
System.out.println(decimalFormat.format(3.1000565));
相關問題
- 1. Javascript to fixed功能
- 2. fixed divs to fluid div
- 3. Full height div to fixed
- 4. MPAndroidChart MarkView in fixed potion
- 5. Html position fixed bugs screen to right
- 6. Float right fixed svg in div
- 7. position fixed in IE9 content in div loaded via ajax
- 8. bootstrap static to fixed navbar jumping on scroll
- 9. position fixed div in scrollable div - can not scroll
- 10. setStyleAttribute position fixed in ie 9 with google apps script
- 11. Bootstrap navbar fixed right
- 12. Xamarin MasterDetailPage fixed Toolbar
- 13. Sherlock ActionItems unresponsive FIXED
- 14. Fixed - AJAX and datalists
- 15. GWT Celltable fixed columns
- 16. Scrollable-fixed div
- 17. Fixed Positioned Div Inside Div
- 18. Bootstrap fixed col-md-4
- 19. CSS:position = fixed and height = auto
- 20. `fixed`與GCHandle.Alloc(obj,GCHandleType.Pinned)
- 21. Can not Center Fixed Div
- 22. Bootstrap3 navbar-fixed-top height
- 23. android camera clickat fixed frequency
- 24. postgresql:INSERT INTO ...(SELECT * ...,「fixed value」)
- 25. CSS position fixed center sibling
- 26. jQuery get url not fixed/static
- 27. Bootstrap with navbar fixed top and Bootstro
- 28. div div div content fixed on scroll?
- 29. Datatable Fixed Column(Right Only)問題
- 30. jQuery SlideDown with dynamic fixed center
'String data =「10223455.3415」; BigDecimal decimal = new BigDecimal(data);的System.out.println(decimal.setScale(2,BigDecimal.ROUND_HALF_UP).toPlainString());」正在爲我工作 – jayanth
也許這行代碼可以幫助你'System.out.println(String.format(Locale.ENGLISH,「%.2f」,1.23456));' –