In delfi there are functions trunc, frac, round
and mn. Are there any corresponding analogues in Java? Particularly interested in Frac
.
2 answers
import java.math.*; double b = 3.1415926; trunc (целая часть числа) - int a = (int)b; fruc (дробная часть числа) - double a = ((int)b) - b; round (округление) - int a = Math.round(b);
Something like this...
- fruc (fractional part of the number) - double a = ((int) b) - b; apparently b - ((int) b)? - Ivan
- oh well yes In fact, even: double a = Math.abs (b) - ((int) b); - Alex Kapustin
|
In addition to samopisnyh options, I can also recommend math functions from Apache Commons, as well as standard variations of these methods, which are based on the use of BigDecimal
and the setScale
method.
If these functions are not used in calculations, but are intended to be used, for example, when displayed on a screen, then we can limit ourselves to using DecimalFormat.
More details can be found here.
|