Here is your answer:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "Your input";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
This only affects NUMBERS, not strings, as you asked.
Try adding that in your main method. Or try this one, it receives input:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
System.out.println("Value?: ");
Scanner scanIn = new Scanner(System.in);
String str = scanIn.next();
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
Answer from Whippet on Stack OverflowHere is your answer:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "Your input";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
This only affects NUMBERS, not strings, as you asked.
Try adding that in your main method. Or try this one, it receives input:
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
System.out.println("Value?: ");
Scanner scanIn = new Scanner(System.in);
String str = scanIn.next();
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
The easiest way is to use two regexes. The first to make sure it is numeric (something along the lines of [0-9.,]*), and the second to clean it (result.replaceAll("/,//"))
exception - How to parse number string containing commas into an integer in java? - Stack Overflow
java - Removing Dollar and comma from string - Stack Overflow
java - To remove commas the end of list of numbers - Stack Overflow
java - how to remove a comma in a string - Stack Overflow
Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:
NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")
This results in 265.858. But using US locale you'll get 265858:
NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")
That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.
If these are two numbers - String.split() them and parse two separate strings independently.
You can remove the , before parsing it to an int:
int i = Integer.parseInt(myNumberString.replaceAll(",", ""));
do like this
NumberFormat format = NumberFormat.getCurrencyInstance();
Number number = format.parse("\$123,456.78");
System.out.println(number.toString());
output
123456.78
Try,
String liveprice = "$123,456.78";
String newStr = liveprice.replaceAll("[$,]", "");
replaceAll uses regex, to avoid regex than try with consecutive replace method.
String liveprice = "$1,23,456.78";
String newStr = liveprice.replace("$", "").replace(",", "");
In this cases I use a simple trick:
String SEPARATOR = "";
for(i = 0; i < n; i++) {
data[i] = input.nextInt();
}
for(i = 0; i < n; i++) {
if((i + 1) % 2 == 0) {
System.out.print(SEPARATOR + data[i]);
sum += data[i];
SEPARATOR = ",";
}
}
You can also use the ternary operator to do it like this
for (int i = 0; i < n; i++) {
if ((i + 1) % 2 == 0) {
System.out.print(data[i] + i != n-1 ? "," : "");
sum += data[i];
}
}
public static void main(String args[]) throws IOException
{
String regex = "(?<=[\\d])(,)(?=[\\d])";
Pattern p = Pattern.compile(regex);
String str = "John loves cakes and he always orders them by dialing \"989,444 1234\". Johns credentials are as follows\" \"Name\":\"John\", \"Jr\", \"Mobile\":\"945,234,1110\"";
Matcher m = p.matcher(str);
str = m.replaceAll("");
System.out.println(str);
}
Output
John loves cakes and he always orders them by dialing "989444 1234". Johns credentials are as follows" "Name":"John", "Jr", "Mobile":"9452341110"
This regex uses a positive lookbehind and a positive lookahead to only match commas with a preceding digit and a following digit, without including those digits in the match itself:
(?<=\d),(?=\d)
Java float doesn't have that much precision, which you can see with
float f = 23000.2359f;
System.out.println(f);
which outputs
23000.236
To get the output you want, you could use a double like
double d = 23000.2359;
String v = String.valueOf(d).replace(".", "");
int val = Integer.parseInt(v);
System.out.println(val);
Output is (the requested)
230002359
you must find a way to get the number of digit after decimal place 1st. Suppose it is n. then multiply the number with 10 times n
double d= 234.12413;
String text = Double.toString(Math.abs(d));
int integerPlaces = text.indexOf('.');
int decimalPlaces = text.length() - integerPlaces - 1;
You can modify the loop
for(int i = 0; i < numbers.length - 1; i++) {
System.out.print(numbers[i] + ",");
}
System.out.print(numbers[numbers.length - 1]);
Or use streams
Arrays.stream(numbers).collect(Collectors.joining(","));
StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Here in this case we are using the delimiter as ,
int numbers[] = {23, 79, 41, 68, 17, 39, 51, 75, 95, 19};
StringJoiner stringJoiner = new StringJoiner(",");
System.out.print("Integer values: ");
for (int i = 0; i < numbers.length; i++) {
stringJoiner.add(String.valueOf(numbers[i]));
}
System.out.println(stringJoiner);
There is a caveat that you need to be aware about since you're trying to use an intermediate int variable:
- The range of
doublevalues is far broader than the range oflong(andintobviously). So by converting adoubleinto alongand then again todoubleyou might lose the data.
Here are some ways how it can be done without losing the data:
1. Modular division:
double num = 59.012;
double wholeNum2 = num - num % 1;
2. Static method Math.floor():
double num = 59.012;
double wholeNum = Math.floor(num);
3. DecimalFormat class, that allow to specify a string pattern and format a number accordingly:
double num = 59.012;
NumberFormat format = new DecimalFormat("0");
double wholeNum = Double.parseDouble(format.format(num)); // parsing the formatted string
All examples above will give you the output 59.0 is you print the variable wholeNum.
When you need to obtain a double value as a result with the fractional part dropped, options 1 and 2 are preferable. But a string representing this number will still contain a dot and one zero .0 at the end.
But if you need to get the result as a String containing only the integer part of a double number, then DecimalFormat (as well String.format() that was mentioned in the comments) will help you to get rid of the fractional part completely.
NumberFormat format = new DecimalFormat("0");
System.out.println(format.format(59.012));
Output:
59
This is what i would do.
double num1 = 2.3;
double num2 = 4.5;
Later...
int num11 = (int) num1;
int num22 = (int) num2;
System.out.println(num11 + ", " + num22 + ".");
The result would be:
2, 4.
That's all.