Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In English, truncate means to trim or cut something. Truncation in Java programming refers to removing certain float or double-type digits or characters from the right of a string. We can also remove all of the decimal points that convert it to an integer. Keep in mind that the number won't be rounded to the nearest value after truncation. Truncation is a method of approximation of a result.
When division with integers is required, and the results must also be integers, it is frequently used in computing (particularly in databases and programming)
If you are truncating a positive value (greater than or equal to zero) or a negative value, the process will vary slightly (less than zero).
Let's just talk about the algorithm for truncating positive values to keep things simple (and the really motivated among you can see if you can figure out an algorithm for truncating negative numbers).
Keep in mind that for every given return value, the domains of the truncation function and round function are offset by precisely 0.5. Amazing, huh?
For example, if I had a number of 5.75 that would truncate to 5, I could simply remove 0.5 from it to get a new value of 5.25, which would round to 5. So, to truncate a positive value, just deduct 0.5 from it, round the result, and you're done.
Now let's perform truncation in Java.
Methods to perform truncation in Java
An array of single-dimensional arrays makes up a two-dimensional array in Java. A two-dimensional array will contain one set of columns for each one-dimensional array.
Method 1 - Mathematically
By multiplying 10^ n, move the decimal of the given number to the chosen decimal point.
Divide the amount by 10^n after taking the floor of the number.
The truncated value is the final value.
Let's see the implementation of the above method.
Code Implementation
Java
Java
import java.io.*;
class CodingNinjas {
static void change(double num, int decimal) {
// 10 ^ n. double dp=Math.pow(10, decimal);
// Multiplying as told in step 1. num *=dp ; num = Math.floor(num);
// Dividing. num = num / dp;
System.out.println(num);
return; }
public static void main(String[] args) {
double num1 = 132.692691; int decimal = 4;
change(num1, decimal);
num1 = 92.38953; decimal = 3;
change(num1, decimal); } }
You can also try this code with Online Java Compiler
In Java, Truncation is a method of removing certain float or double-type digits or characters from the right of a string. We can also remove all decimal points that convert it to an integer. Remember that the number won't be rounded to the nearest value after truncation. Truncation in Java is a method of approximation of results.
How truncation in Java is different from rounding off?
Truncation is essentially the same as rounding a decimal value to the nearest whole number, with the exception that rounding off is done to 2 decimal places.
Is there a way to truncate a number using Math functions in Java?
Yes, Java provides the Math.floor() and Math.ceil() methods for truncation. For positive numbers, Math.floor() truncates towards zero, while Math.ceil() truncates towards zero for negative numbers.
What happens when truncating a negative number?
When truncating a negative number, the Math.floor() method rounds down (further from zero), while Math.ceil() rounds up (closer to zero). For example, truncating -3.7 with Math.floor() results in -4, while Math.ceil() gives -3.
Conclusion
In this article, We talked about truncation in Java. It is a useful technique for simplifying numbers by removing their fractional parts. Using methods like Math.floor(), Math.ceil(), and typecasting, developers can efficiently handle various truncation needs, whether for positive or negative numbers. Understanding the behavior of these methods helps ensure accurate and predictable results in mathematical and computational operations.