Java is commonly asked in tech interviews, and thus it's important for us to study Java in detail. Out of all asked topics, Truncation in Java is one of the most asked topics.
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)
Also See, Multithreading in java, and Duck Number in Java.
How to do truncation?
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.
-
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
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);
}
}
Output
132.6926
92.389
Practice by yourself on java online compiler.
-
String matching approach
- Firstly, we will change the given number to a string.
- We will initialize the counter variable when the String contains "."
- Now, The counter will be increased until the decimal point.
- The new String will be saved and parsed in double format.
Let's see the implementation of the above
Code
import java.io.*;
class CodingNinjas {
public static void main(String[] args)
{
double num = 132.692691;
int decimal = 4;
// Convert the number to string.
String sv = "" + num;
String val = "";
int count = -1;
int i=0;
while(i<sv.length()) {
// If crosses the decimal.
if (count > decimal) {
break;
}
else if (sv.charAt(i) == '.') {
count = 1;
}
else if (count >= 1) {
count++;
}
// Number is converted to a string.
val += sv.charAt(i);
i++;
}
// Parsing and storing as double.
double nv = Double.parseDouble(val);
System.out.println(nv);
}
}
Output
132.6926