Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

String in Double

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

Gibt es in Java eine Klasse mit der ich einen String (Bsp. 100.000.000,99)

in einen Double oder BigDecimal umwandeln kann, ohne dass ich den String

vorher mit replace() bearbeiten muss?

Die umgekehrte Methode kenne ich, dass man einen Double über DecimalFormat

in einen String umwandeln kann.

Aber wie bekomme ich einen gültigen Double oder besser BigDecimal zurück?

Lässt sich das über die Number-Klasse irgendwie bewerkstelligen??

Danke im Voraus

Krain

Also normalerweise konvertiert man mit Double.parseDouble(arg0)

aber wenn Da Kommas oder zu viel Punkte drin sind birngt er ne NumberFormat Exception.

MfG Spoom

  • Autor

Ja - das weiß ich ja.

Ich dachte, dass das vielleicht über die Locale funktionieren könnte.

Natürlich müsste der String auch ein gültiges Format haben.

Wie in DE '.' für Tausendertrennung

und ',' für Nachkommastellen.

Schreib dir halt ne methode MyParseDouble() und da machst DU die Punkte weg und aus nem Komma machst Du nen Punkt ...

MfG Spoom

  • Autor

Danke - Das hab ich ja schon. Aber meinem Projektleiter gefällt das so nicht ganz.

Deswegen sollte ich nach einer 'besseren' Methode suchen.

Aber mir fällt nichts gescheites mehr dazu ein.

Naja Java ist "internationla" deswegen glaub ich kaum, das die sich an Deutsche konventionen halten....

Das ist wie mit den Datums Formaten ...

MfG Spoom

Also raten bringt ziemlich wenig. :rolleyes:

DecimalFormat df = new DecimalFormat();

Number n = df.parse("1.000.000.000,99");

double x = n.doubleValue();

DecimalFormat:

Creates a DecimalFormat using the default pattern and symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.

oder:

NumberFormat df = NumberFormat.getInstance();

Number n = df.parse("1.000.000.000,99");

double x = n.doubleValue();

NumberFormat:

Helps you to format and parse numbers for any locale.

Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

Gruß Jaraz

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.