Używam doFixed (2) po stronie klienta (javascript). Ale po stronie serwera (java) muszę ponownie przeliczyć. jak mogę to zrobić?Javascript toFixed equivalent in java
5
A
Odpowiedz
7
setScale()
z BigDecimal
zrobi to za Ciebie na stronie serwera
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 „s format metoda jest równoważne w Javie do toFixed w javascript.
DecimalFormat decimalFormat = new DecimalFormat("0.##");
System.out.println(decimalFormat.format(3.1000565));
Powiązane problemy
- 1. C# DateTime.Ticks equivalent in Java
- 2. password_hash equivalent in nodejs
- 3. Javascript toFixed function
- 4. nie .toFixed dla .0 *
- 5. Jquery input.files equivalent
- 6. Python #define equivalent
- 7. Jak mod2^64 in long in Java?
- 8. Javascript onload in HTML
- 9. Python's __getattr__ in JavaScript
- 10. function()() in javascript
- 11. String isNullOrEmpty in Java?
- 12. Biased locking in java
- 13. Ruby file_get_contents equivalent
- 14. Python foreach equivalent
- 15. dispatch_after equivalent w NSOperationQueue
- 16. Angular 2 - ngShow equivalent?
- 17. Co to jest .NET Equivalent of InterruptedException (Java)?
- 18. JavaScript Just In Time compilation
- 19. Zgodność z operatorem JavaScript IN
- 20. ++ someVariable Vs. someVariable ++ in JavaScript
- 21. toFixed (2) runduje "x.525" niekonsekwentnie?
- 22. Dlaczego Firebug mówi toFixed() nie jest funkcją?
- 23. Eclipse Java Projects in Git
- 24. Concat two ByteBuffers in Java
- 25. jquery like lib in java
- 26. Java HashSet equiv in C++
- 27. Python pdb (debugger) disp equivalent?
- 28. Rails "before_filter equivalent w Phoenix
- 29. Scikit-learn equivalent for C++?
- 30. Visual Studio Code $ psise equivalent
'Dane ciągu = "10223455.3415"; BigDecimal decimal = new BigDecimal (data); System.out.println (decimal.setScale (2, BigDecimal.ROUND_HALF_UP) .toPlainString()); ' działa dla mnie – jayanth
Może ta linia kodu pomaga 'System.out.println (String.format (Locale.ENGLISH,"% .2f ", 1.23456));' –