Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change decimal separator in double

Status
Not open for further replies.

Madere

Technical User
Apr 14, 2003
85
GB
Hi,

I want to change the decimal separator in a double. The result MUST be a double value with the needed new decimal separator.

Input: 10,0
Output: 10.0

How to change the "," into an "." where the local computer settings is put to DE. Changing the local settings of the computer is NOT an option. The change has to be done inside the Java code (and JVM).

Thanks.
 
Hi

Must the output value be of type double ? I thin it would be better to convert it to [tt]String[/tt] and do a [tt]replace()[/tt].
Code:
[b]double[/b] d = 3,14;
System.out.println( ( [i]""[/i] + d ).replace( [i]','[/i], [i]'.'[/i] ) );

Feherke.
 
Wouldn't you hold your doubles as native double values and use locale-specific java.text.DecimalFormat instances to convert user input / output?

Tim
 
Feherke,
as I wrote: "The result MUST be a double value", so your solution is NOT an option.

Tim,
I have tried something like that, but the output is still a String and not a double/Double value. I also tried to change the Locale.setDefault(Locale.UK), but for some reason I still have the wrong separator.
 
You process your numbers as numerical values. Things like separators are human consumable decoration and should only be applied/processed at the point of input / display. Once the number is 'in' your program it is a plain double.

Tim
 
Feherke:
to me the output is irrelevant.
As in your example, the result should be:
double d_new = 3.14;
with your
double d = 3,14;
used as input.

Tim:
the new double value has to be written into a class field which is of type double. At the moment of writing the "." must be there, because I dont wanna change the data anymore in a later stage. At this stage it has nothing to do with "human consumable decoration".
 
The point I'm trying to make, Madere, is that this is no such thing as a decimal separator in a double, only in the human expression of that double. A double is a pure mathematical construct. Whatever characters are used to represent, visually, decimal separators and/or the decimal point is a purely human concern. You can't talk about using a comma as the separator in a double, only in the formatted representation of a double.

Tim
 
try this:
Code:
 import java.text.*;
import java.util.*;

public class Dezimal
{
	public Dezimal (String param)
	{
		double dd = 0.0;

		NumberFormat df = NumberFormat.getInstance (new Locale ("de", "DE"));
		if (df instanceof DecimalFormat) 
		{
			dd = (Double) df.parse (param, new ParsePosition (0));
		}
	}

	public static void main (String args[])
	{
		String param = "1.234.456,78";
		if (args.length == 1)
		{
			param = args[0];
		}
		new Dezimal (param);
	}
}


don't visit my homepage:
 
Stefan,

thanks.
That brings me further.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top