Hi All,
I need to trunk a float number with one decimal place. For eg: i have a float number called total=95.20004. my result should be 95.2. I have tried with the DecimalForma, but it is not working out. Kindly help me to resolve this issue.
Hi All,
I need to trunk a float number with one decimal place. For eg: i have a float number called total=95.20004. my result should be 95.2. I have tried with the DecimalForma, but it is not working out. Kindly help me to resolve this issue.
Short note summarizing the thread and a safe way to truncate (not round) to one decimal place. Several replies correctly pointed out formatting versus changing the stored value ( showed printing options; separated printing-from-storing; flagged precision issues). For display only, formatters are fine. For a numeric truncation that actually chops off extra digits, use a decimal-aware approach or a math shortcut.
A robust approach: BigDecimal with RoundingMode.DOWN (truncates toward zero). Use BigDecimal.valueOf(double) to avoid binary-float artifacts, then setScale(1, RoundingMode.DOWN). Example:
import java.math.BigDecimal;
import java.math.RoundingMode;
double total = 95.20004;
double truncated = BigDecimal.valueOf(total)
.setScale(1, RoundingMode.DOWN)
.doubleValue();
// or produce a clean string for display:
String out = BigDecimal.valueOf(total)
.setScale(1, RoundingMode.DOWN)
.toPlainString(); A simple arithmetic alternative (fast for positive numbers) uses Math.floor:
double n = 95.20004;
double truncated = Math.floor(n * 10.0) / 10.0; // for positives
// truncate toward zero (handles negatives correctly)
double truncTowardZero = (n >= 0)
? Math.floor(n * 10.0) / 10.0
: Math.ceil(n * 10.0) / 10.0; Practical tips: DecimalFormat/String.format/printf are for presentation and will usually round rather than truncate. BigDecimal is preferable for financial or exact decimal work (it is slower but precise). When comparing doubles, avoid exact equality — compare with an epsilon or compare formatted strings.
Jump to Post— tux4life 2,072You should be more specific as to what you want to do with your truncated number.
Do you want to put it in aString? Do you want to print it to the screen and keep the exact value in your variable? Do you want to truncate the result in …
From the java docs:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DecimalFormat.html
A similar (answered) question:
I think you getting a error like
possible loss of precision
Because you have a float number called total=95.20004
But actuallly the total is not float it is double value.
First you take total as a double value and use
DecimalFormat(##.#)
on it you will get desired output OR
It you want total as float then take total=95.20004f
try this code
import java.text.DecimalFormat;
public class DecimalPlaces {
public static void main(String[] args) {
float f = 95.20004f;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(f));
}
}
Hello you can use the printf();
float num = 1.23456;
System.out.printf("the float number is: %.1f",num);
%f prints the full float number
%.1f prints 1 decimal
%.2f prints 2 decimal
%.3f prints 3 decimal
etc ....
You should be more specific as to what you want to do with your truncated number.
Do you want to put it in a String? Do you want to print it to the screen and keep the exact value in your variable? Do you want to truncate the result in your double variable?
System.printf("%.1f", number);String: String.format("%.1f", number); and assign it to a String reference variable.double variable: number = (int) (number * 10) / 10.0;We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.