Hi a service I am using returns prices in the form 00003600 or 00013650
I would like to transform this to a string of the form 36.00 Euro or 136.50 Euro.
Can this be done using regex.
Thanks,
just parse the string and then divide by 100. Or use BigDecimal and scale.
In case the other answers were not clear enough
String text = "00003600";
BigDecimal value = new BigDecimal(text).scaleByPowerOfTen(-2);
System.out.println(value);
prints
36.00
OR
String text = "00003600";
double value = Double.parseDouble(text) / 100;
System.out.printf("%.2f%n", value);
prints
36.00
Most correct way (IMHO):
String input = "00013650";
BigDecimal value = BigDecimal.valueOf(Long.parseLong(input), 2);
String output = value.toPlainString();
System.out.println(output);
Outputs:
136.5
Related
This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I have data like this for price number from my broker:
String Price = "00000001970.00";
String Price1 = "0000000295.00";
This is the code I tried to use:
String Price2 = price.replace ("0", "");
But result was Price2 = 197. and my expectation is Price2=1970.
Can someone tell me how to fix this problem?
String price = "00000001970.00";
// If you don't care about the decimals
System.out.println(price.split("\\.")[0].replaceFirst("^0+(?!$)", ""));
// If you do care about the decimals:
System.out.println(price.replaceFirst("^0+(?!$)", ""));
See How to remove leading zeros from alphanumeric text?
You need to use a Regex like this:
yourstring.replaceFirst("^0+(?!$)", "");
guy. There was a sea of solutions to resolve your problem. For instance, you can look up like below:
` String price = "00000001970.00";
Double dv = Double.valueOf(price);
System.out.println(dv);
int intValue = dv.intValue();
System.out.println(intValue);
`
And you will get a result like below:
`
1970.0
1970`;
Besides, you can also look up like below:
` String price = "00000001970.00";
String substring = price.substring(0,price.indexOf("."));
System.out.println(Integer.valueOf(substring));
`
I'm personally not a big fan of regexes so I avoid them whenever possible.
Assuming that all your Strings are numeric, you could parse them to BigDecimal and then print that however you want. BigDecimal is prefer over Double if you care about precision.
To achieve what you want you could do something like so:
public static void main(String args[]) {
String price = "00000001970.00";
String price1 = "0000000295.0011";
BigDecimal num = new BigDecimal(price);
BigDecimal num1 = new BigDecimal(price1);
DecimalFormat df = new DecimalFormat("#.###");
System.out.println(df.format(num));
System.out.println(df.format(num1));
}
Those numbers will be formatted as so:
1970
295.001
To learn more about possible formats read DecimalFormat Javadocs.
Double#parseDouble
public class Main {
public static void main(String[] args) {
String strPrice = "00000001970.00";
double price = Double.parseDouble(strPrice);
System.out.println(price);
}
}
Output:
1970.0
The simplest way seems to use method replaceFirst from class org.apache.commons.lang3.RegExUtils (Apache Commons):
String price = RegExUtils.replaceFirst("00000001970.00", "0+(\\d)", "$1");
This will replace a substring of first zeros followed by any digit by this digit.
i want to format my double value to 2 decimals and then make it "text to speech".
this is my code:
mares = mass * acc;
DecimalFormat df = new DecimalFormat("#.00");
df.format(mares);
String mare = String.format("The force is %f", df);
home.speak(mare,TextToSpeech.QUEUE_FLUSH, null);
but it crashes, i don't know why, i put 5 and 6 and it should multiply them and give me 30.00 or something like that.
when i remove DecimalFormat the result is 30.00000000000000, i just don't like it, too many zeros.
can someone help me please?
Thanks in advance!
Your DecimalFormat is returning the formatted string, but you are ignoring it, and passing it as an argument to String.format, which certainly isn't right.
Assign the return of df.format to a string for further reference:
String mare = df.format(mares);
Or pass the numeric value directly to String.format, with the appropriate format precision specified:
String mare = String.format("The force is %.2f", mares);
I am trying to get 123456789.12 value out of 123456789.1234
If i use strings, it will be worked straightaway using parsing logic. I don't want to return String. requirement is to return Long.
Here i would like return the output the Long type rather than string..can someone help me on this?
Long is an integer type, and thus can not hold a decimal.
Trying to use Long.parseLong would result in a NumberFormatException.
You need to use a representation that deals with the scale as well.
try this for long output:
String str = "123456789.1234";
BigInteger num = new BigInteger(str);
return num.longValue();
try this for float output:
String str = "123456789.1234";
BigDecimal num = new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP);
return num.floatValue();
The number which you want to return it is not Long.You can return double or float.Also you can use
DecimalFormat df = new DecimalFormat("#.##");
String stringDouble = df.format(yourDouble);;
i have a string object
String s = "64.5369474 British pounds"
what i want is to have a methos that can get the digits out and transfer into 2 decimal place,
the result i expect to get is something like:
String result = "64.54 British pounds"
any suggestions?
I'll guide you but will not show you a full solution. One way is to:
Split the String to extract the number - See String#split.
Convert the first String (Here I assume that the format is fixed) using Double#parseDouble.
Use DecimalFormat to truncate the number.
The result String can be easily reconstructed.
DecimalFormat df = new DecimalFormat("00.00");
String unformatedMoney = "65.432784327489";
String formattedMoney = df.formart(unformattedMoney);
System.out.println(formattedMoney + " British pounds");
I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example:
BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out = d.toString(); // Or perform any formatting that needs to be done
System.out.println(out);
I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16.
To preserve the precision for a BigDecimal you need to pass the value in as a String
BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901");
System.out.println(d.toPlainString());
The BigDecimal class has a toPlainString method. Call this method instead of the standard toString and it will print out the full number without scientific notation.
Example
BigDecimal b = new BigDecimal("4930592405923095023950238502395.3259023950235902");
System.out.println(b.toPlainString());
Output: 4930592405923095023950238502395.3259023950235902
You want to use a DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
String output = df .format(myBD);
System.out.println(value + " " + output);
You could use this
BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out= d.toPlainString();
System.out.println(out);