Table of contents
1.
Introduction
2.
How to do truncation?
3.
Methods to perform truncation in Java
3.1.
Method 1 - Mathematically
3.2.
Java
3.3.
Method 2: String matching approach
3.4.
Java
4.
Frequently Asked Questions
4.1.
What is the meaning of truncation in Java?  
4.2.
How truncation in Java is different from rounding off?
4.3.
Is there a way to truncate a number using Math functions in Java?
4.4.
What happens when truncating a negative number?
5.
Conclusion
Last Updated: Dec 12, 2024
Medium

What is truncation in Java?

Author Saksham Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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)

Introduction

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.

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.
Method 1

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
Run Code

Output

132.6926
92.389

Practice by yourself on java online compiler.

Method 2: 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 Implementation 

  • Java

Java

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);
}
}
You can also try this code with Online Java Compiler
Run Code

Output

132.6926

Frequently Asked Questions

What is the meaning of truncation in Java?  

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. 

Recommended Articles: 

Live masterclass