This line is your problem:
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
There you formatted your float to string as you wanted, but but then that string got transformed again to a float, and then what you printed in stdout was your float that got a standard formatting. Take a look at this code
import java.text.DecimalFormat;
String stringLitersOfPetrol = "123.00";
System.out.println("string liters of petrol putting in preferences is "+stringLitersOfPetrol);
Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
stringLitersOfPetrol = df.format(litersOfPetrol);
System.out.println("liters of petrol before putting in editor : "+stringLitersOfPetrol);
And by the way, when you want to use decimals, forget the existence of double and float as others suggested and just use BigDecimal object, it will save you a lot of headache.
Answer from David Hofmann on Stack OverflowThis line is your problem:
litersOfPetrol = Float.parseFloat(df.format(litersOfPetrol));
There you formatted your float to string as you wanted, but but then that string got transformed again to a float, and then what you printed in stdout was your float that got a standard formatting. Take a look at this code
import java.text.DecimalFormat;
String stringLitersOfPetrol = "123.00";
System.out.println("string liters of petrol putting in preferences is "+stringLitersOfPetrol);
Float litersOfPetrol=Float.parseFloat(stringLitersOfPetrol);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
stringLitersOfPetrol = df.format(litersOfPetrol);
System.out.println("liters of petrol before putting in editor : "+stringLitersOfPetrol);
And by the way, when you want to use decimals, forget the existence of double and float as others suggested and just use BigDecimal object, it will save you a lot of headache.
Java convert a String to decimal:
String dennis = "0.00000008880000";
double f = Double.parseDouble(dennis);
System.out.println(f);
System.out.println(String.format("%.7f", f));
System.out.println(String.format("%.9f", new BigDecimal(f)));
System.out.println(String.format("%.35f", new BigDecimal(f)));
System.out.println(String.format("%.2f", new BigDecimal(f)));
This prints:
8.88E-8
0.0000001
0.000000089
0.00000008880000000000000106383001366
0.00
When you give String.format("%02x",...), you are telling the compiler to give you a Hexadecimal number.
But then you are trying to parse it as a decimal number. Hence the issue.
You should do either of these,
file.write(Integer.parseInt(String.format("%02d", Integer.reverseBytes(str)), 10));
or,
file.write(Integer.parseInt(String.format("%02x", Integer.reverseBytes(str)), 16));
You can skip formatting and parsing, and go straight for this implementation:
file.write(Integer.reverseBytes(str));
The problem is that you are printing the number as hex, but you parse it back as a decimal. To parse the number back as hex, pass 16 for the last parameter of parseInt:
file.write(Integer.parseInt(String.format("%02x", Integer.reverseBytes(str)), 16));
// ^^
Demo.
Note: This is probably not what you want anyway: if you with to write all four bytes, not the MSB, do this:
byte[] bytes = ByteBuffer
.allocate(4)
.putInt(Integer.reverseBytes(str))
.array();
file.write(bytes);
IsNumeric() method works for only Integer value not for decimal value
like this
String str = '10';
system.debug('===strToDec=='+str.isNumeric());
This will return true.
String str = '10.25';
Decimal strToDec = decimal.valueOf(str);
system.debug('===strToDec=='+strToDec);
String decToStr = String.valueOf(strToDec);
system.debug('===decToStr=='+decToStr);
Run this code in developer console ad check output.
In you case do something like. First convert string to numeric then check and again convert string into decimal for future use
String str = '10.25';
Integer intCheck = Integer.ValueOf(str);
Decimal decVal = Decimal.ValueOf(str);
if(String.ValueOf(intCheck ).isNumeric())
//use decVal here
Don't understand why you need this.
As Nick Cook suggested, this is one of the rare cases when catching the exception is probably the cleanest approach:
Object o = ...
Decimal d;
try {
d = Decimal.valueOf(String.valueOf(o));
} catch (TypeException e) {
d = null;
// In a controller you might add the exception message e.g. "Invalid decimal: abc"
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
Use Integer.parseInt (see javadoc), that converts your String to int using base two:
int decimalValue = Integer.parseInt(c, 2);
public static int integerfrmbinary(String str){
double j=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)== '1'){
j=j+ Math.pow(2,str.length()-1-i);
}
}
return (int) j;
}
This piece of code I have written manually. You can also use parseInt as mentioned above . This function will give decimal value corresponding to the binary string :)
Yes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);