How to get a numeric float (not a string!) When json_encoding?

I need to have a numeric float using a dot delimiter that remains numeric after (json) encoding to send POST headers to a third-party API.

However, you have tried for several hours but cannot make it work.

What I need:

{
    "price": 17.95,
    // BUT NOT:
    "price": "17.95" OR 17,95
}

Why? . Since the endpoint of the receiving API does the check for the first, but throws an error non-numericfor the last two values.

Now we are in Holland. Therefore, our "locale" uses a comma delimiter. Working around this, setting the locale from nl_NLto en_US, the function number_formatprovides the correct format, however, like a string.

(float) . ("17.95" 17)

- , cURL. POST , . :

$client->updateShopItem($shopId, $articleNumber, $updateArray)

$shopId= int $articleNumber= $updateArray=

, , :

$client->updateShopItem(12345, "a1b2c3", [
    "price" => 17.95,
    "sale_price" => 12.99,
    //... other values
]);

, , : "17,95".

:

$price = "17,95" //Starting point (locale = nl_NL)

number_format($price, 2) // "17.00" - incorrect type and value
number_format(str_replace(',', '.', $price), 2) // "17.95" - string
(float) str_replace(',', '.', $price); // 17,95 - comma
(float) number_format(str_replace(',', '.', $price), 2) //17,95 - comma

setLocale(LC_ALL, 'en_US'); //Changing locale here, US uses dot separator

$check = locale_get_default(); // "en_US"

number_format($price, 2) // "17.00" - incorrect type and value
number_format(str_replace(',', '.', $price), 2) // "17.95" - string
(float) number_format(str_replace(',', '.', $price), 2) // 17,95 - comma
(float) str_replace(',', '.', $price); // 17,95 (can't figure why comma with US locale)

, , json_encode() /:

json_encode(["test1" => "17,95", "test2" => 17.95]);

//Results in: 
{"test1":"17,95","test2":17.95}

: : , , . se

setlocale(LC_ALL, 'nl_NL');
ini_set('intl.default_locale', 'nl-NL');
$update = [
    'price'             => ((float)number_format(str_replace(',', '.', $prijs), 2)),
    'discount_price'    => (float) str_replace(',', '.', $actieprijs),
];

setlocale(LC_ALL, 'en_US');
ini_set('intl.default_locale', 'en-US');
// Update a shopitem
$update2 = [
    'price'             => ((float)number_format(str_replace(',', '.', $prijs), 2)),
    'discount_price'    => (float) str_replace(',', '.', $actieprijs),
];

enter image description here

2: , @KevinStich , , , Windows.

, , set:

if (!setlocale(LC_ALL, 'en_US.utf8')) { //Works on "normal" server/Linux stuff
    setlocale(LC_ALL, 'us'); //Windows is special
}

, setlocale() false, , . , @KevinStich .

+4
1

@Nukeface , setlocale() ; , Windows unix.

setlocale return , , localeconv(), .

, json_encode() 'd, setlocale , setlocale .

, :


, - . , , .

REPL

var_dump(
    ((float)number_format(str_replace(',', '.', $price), 2))
);
float(17.95)

, json_encode:

$price = "17,95";
$a = array();
$a[] = $price;
$a[] = ((float)number_format(str_replace(',', '.', $price), 2));

echo json_encode($a); // Prints ["17,95",17.95]
+2

Source: https://habr.com/ru/post/1662279/


All Articles