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.

php easy auf schlauch 200 zu 200.00

Empfohlene Antworten

Veröffentlicht

hi und hallo,

ich möchte gerne aus integer zahlen gleitkommazahlen mit der präzision 2 nachstellen machen.

also aus

200 200.00

300 300.00

usw. die selbe funktion soll mir aber wenn es bereits eine gelitkommazahl ist auch "normal" auf 2 stellen runden

also

222,3456 = 222,35

100,138 = 100,14

den zweiten fall krieg ich mit round() hin, aber beim ersten dache ich an doubleval() oder floatval() aber es ging nicht...

welche funktion hilft mir da weiter?

Wenn es immer ein .00 dahinter soll dann mach doch einfach

$zahl .= '.00';

ist wohl die einfachste Lösung :D

nunja wie gesagt, wenn die zahl bereits gleitkomma ist darf natürlich kein .00 stattfinden...

siehe

100,138 = 100,14

Hi

kommazahlen werden mit "." getrennt!


function toFloat( $no) {
if ( strpos( $no, '.') <= 0 ) {
$no .= '.00';
}

return round( $no, 2);
}
[/PHP]

ungetestet!

Hi

kommazahlen werden mit "." getrennt!


function toFloat( $no) {
if ( strpos( $no, '.') <= 0 ) {
$no .= '.00';
}

return round( $no, 2);
}
[/PHP]

ungetestet!

naja so kann man es auch machen :) nun peil ich auch was die Funktion strpos() macht ;) habs mir nie angeguckt und nun gelernt. Naja hatte gedacht, dass diese Zahlen mit .00 am Ende unabhängig von denen mit den Nachkommastellen sind. Hätte aber wenn auch ne eigene Funktion vorgeschlagen.

ich muss mich nochmal verbessern:


function toFloat( $no) {
// $no .= ''; hier evtl nach String casten
if ( strpos( $no, '.') !== false ) {
$no .= '.00';
}

return round( $no, 2);
}
[/PHP]


function toFloat($no) {
if (strpos(strval($no), '.') !== false)
$no .= '.00';
return round($no, 2);
}
[/PHP]

oder vllt. so? :P

Wie wärs mit


$zahl = 3; // Zahl ist 3
$zahl = number_format($zahl, 2, '.', ''); // Zahl ist 3.00
[/PHP]

Wie wärs mit


$zahl = 3; // Zahl ist 3
$zahl = number_format($zahl, 2, '.', ''); // Zahl ist 3.00
[/PHP]

immer diese besserwisser ;)

Ich schreib mir gleich ne Funktion, nenne sie a() und compiliere die dann in mein PHP rein und dann gehts noch kürzer :)

:P So, Feierabend!

jupp tobisas digital danke das wars was ich suchte. natürlich könnte ich den string manipuliern je nachdem ob ein "." drin ist oder nicht, das schien mir aber zu umständlich. und das isses ja nun auch ;)

vielen dank an alle die sich gedanken gemacht haben!

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.