I would like to replace "." by "," in a String/double that I want to write to a file.
Using the following Java code
double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);
String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);
myDoubleString.replace(".", ",");
System.out.println(myDoubleString);
myDoubleString.replace('.', ',');
System.out.println(myDoubleString);
I get the following output
38.1882352941176
38.1882352941176
38.1882352941176
38.1882352941176
Why isn't replace doing what it is supposed to do? I expect the last two lines to contain a ",".
Do I have to do/use something else? Suggestions?
You need to assign the new value back to the variable.
double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);
String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);
myDoubleString = myDoubleString.replace(".", ",");
System.out.println(myDoubleString);
myDoubleString = myDoubleString.replace('.', ',');
System.out.println(myDoubleString);
The original String isn't being modified. The call returns the modified string, so you'd need to do this:
String modded = myDoubleString.replace(".",",");
System.out.println( modded );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With