Jump to content

Float field type: a way to convert commas into dots?


Christophe
 Share

Recommended Posts

Hello,

I have everything ok now here: 

But I'm wondering if there is a way to convert commas into dots automatically?
For exemple for the field prixAuMetreCarre (float), 166,66 (French) into 166.66 (English).
Is it possible via a setting in the backend or via a hook?

Not especially for now, but I also have the field tarif (integer) for exemple, 115000. This format is ok as it is I think. But if, in the future, I ever need to use (something like) 115.000 (French) instead -> 115,000 (English).
Is it possible via a hook or in any other way?

Link to comment
Share on other sites

There is a topic and a fieldtype to solve problems like this.
https://processwire.com/talk/topic/7542-development-fieldtypefloat-fieldtypedecimal/
http://modules.processwire.com/modules/fieldtype-decimal/

or

$code = $pages->get(1)->localName($user->language);
switch ($code) {
    case 'en':
    setlocale(LC_ALL, 'en_GB.UTF-8');
    break;

    default:
    setlocale(LC_ALL, 'fr_FR.UTF-8');
}

 

  • Like 1
Link to comment
Share on other sites

Internet connection problems yesterday night... and again now. From time to time it happens, more and more... even with the "stability" option.
Some reasons surely being that households now have several hardwares connected to (the) Internet (YouTube, etc.), Internet TV, illegal music and files downloads...
(I'll remove the 2 lines above later.)

http://php.net/manual/en/class.numberformatter.php
""12 345,67" in France and "12.345,67" in Germany." Reading this I thought, "But in France, we use both...".
I'll read the page more carefully.

In usage, sometimes I usually (a bit of contradiction) write 1000 for example (but I wouldn't put (or less often) 1.000 or 1 000 - or perhaps with a smaller space).
But for 1000,64 I would rather write 1.000,64 (or 1 000,64 with a smaller space).
For 1000000, for sure I would write it 1.000.000 or 1 000 000 (with a smaller space if possible). For 1000000,64, same...
I guess it depends also (on) if you write it with a pen, a keyboard... and on other conditions/reasons (readability, etc.).
Perhaps, I often can't stick to one way of doing things, or I am too "international" for some things...

At the same time, I always wrote  cursive letters with a pen (nice if not too quick), then one day for a job, in order to be more readable to another person I started writing sometimes the other way. From then on, it happens (quite often) that I (still) mix both...

But in French I can also write it in Roman characters (small, big, or tiny)...

I have to find (out) how to create a smaller space (again ?) with a keyboard. Perhaps if I select the numbers, there is a Ctrl + Alt + something for that.

A bit like telephone numbers (sometimes with or without spaces, or with dots).

I'm going to eat a bit...

Where is the preferred place(s) to put your piece of code @kixe please?

  • Like 1
Link to comment
Share on other sites

On 13/03/2017 at 4:55 PM, kixe said:

$code = $pages->get(1)->localName($user->language); switch ($code) { case 'en': setlocale(LC_ALL, 'en_GB.UTF-8'); break; default: setlocale(LC_ALL, 'fr_FR.UTF-8'); }

@kixe It's all good for the decimal part, and simple to add. I've just inserted it at the top of tarifs.php, under <?php namespace ProcessWire; ?>.

How can I modify this piece of code in order to also have the formatting applied - for thousands - to French (little space) and English (dot) please?

@Martijn Geerts You (still) have 2 accounts apparently? Isn't it possible to merge them?
Edit: the other one doesn't seem to have data...

Edit 2: looking at how to change the code with the information here: http://php.net/manual/en/class.numberformatter.php

Have all a nice week!

Edit 3: the "formatting applied - for thousands -" should normally work with setlocale, shouldn't it?

Link to comment
Share on other sites

6 minutes ago, Christophe said:

@Martijn Geerts You (still) have 2 accounts apparently? Isn't it possible to merge them?
Edit: the other one doesn't seem to have data...

I assume you are talking about the PW modules directory? I have just deleted the "martijn" account which had no modules.

  • Like 1
Link to comment
Share on other sites

Trying to find how to merge in the best way possible, this

On 13/03/2017 at 4:55 PM, kixe said:

$code = $pages->get(1)->localName($user->language); switch ($code) { case 'en': setlocale(LC_ALL, 'en_GB.UTF-8'); break; default: setlocale(LC_ALL, 'fr_FR.UTF-8'); }

and this (http://php.net/manual/en/function.number-format.php)

<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

 

Link to comment
Share on other sites

I'm trying to replace the setlocale... parts with some number format code but all I'm having is "Server Error" messages.

I'm not advanced enough in php.

Edit: trying to find a solution starting from here: http://php.net/manual/fr/function.localeconv.php
Edit 2: there's also http://php.net/manual/fr/function.nl-langinfo.php, and others...

Edit 3: forget all this. I'll find it one day.
A lot of the solutions (examples) I'm seeing assume that we assign a specific number to a variable.
I have to find out how to assign any number (integer, float...) to a variable.

Link to comment
Share on other sites

17 hours ago, Christophe said:

Where is the preferred place(s) to put your piece of code @kixe please?

I f you prepend an _init.php to each template this would be a good place to switch the locale.

11 hours ago, Christophe said:

Trying to find how to merge in the best way possible, this

There is no need to merge setlocale and number_format. Since the output after changing the locale isn't enough for your needs I would go on with number_format. I don't know if &thinsp; for the smaller space as thousands separator is well supported by browsers (fonts). So I would try the following:

$code = $pages->get(1)->localName($user->language);
switch ($code) {
  case 'en':
  $number = number_format($page->prixAuMetreCarre, 2, '.', '');
  break;

  case 'fr':
  $number = number_format($page->prixAuMetreCarre, 2, ',', '<span style="font-size: 0.3em;">&nbsp;</span>');
  break;

  case 'de':
  $number = number_format($page->prixAuMetreCarre, 2, ',', '.');
  break;

  default:
  $number = number_format($page->prixAuMetreCarre, 2, ',', '<span style="font-size: 0.3em;">&nbsp;</span>');
}
echo "$number €";

There is also a php function money_format. You maybe want to use this?
http://php.net/manual/en/function.money-format.php

  • Like 2
Link to comment
Share on other sites

Thank you @kixe

I have to go now, I'll look at it when I come back.

Yesterday I continued a little trying to figure it out.

I tried, for example, if I remember well, with things like $number($l->prixAuMetreCarre)... in the foreach directly but it didn't work and I would perhaps have had to change the way if was structured (without <?php endforeach; ?> for instance) without any garanties.

_init.php already exists so I'll put it there.

I need it also for superficie and tarif, so I'll see if something like this works:

$number = number_format($page->superficie|$page->tarif|$page->prixAuMetreCarre, 2, '.', '');
Link to comment
Share on other sites

In fact prixAuMetreCarre doesn't have thousands, but it has floats, so I'll leave it in the code.

It doesn't seem to work (changing decimals and thousands separators)... I'm trying with this in _init.php (and also in tarifs.php):

$code = $pages->get(1)->localName($user->language);
switch ($code) {
  case 'en':
  $number = number_format($page->superficie|$page->tarif|$page->prixAuMetreCarre, 2, '.', ',');
// or $number = number_format($page->superficie|$page->tarif|$page->prixAuMetreCarre); 
as I want commas for thousands in English
  break;

  case 'fr':
  $number = number_format($page->superficie|$page->tarif|$page->prixAuMetreCarre, 2, ',', '<span style="font-size: 0.3em;"> </span>');
// I'm keeping this in case during tests but default should be enough
  break;

  default:
  $number = number_format($page->superficie|$page->tarif|$page->prixAuMetreCarre, 2, ',', '<span style="font-size: 0.3em;"> </span>');
}
echo "$number";

In English, I see this at the top left of the page: 0.000.00
In French I see 0,000,00

I also tried, in case, adding ->children.

Perhaps I should create a template file for the children (and copy or not the code there).

I've tested with return, by curiosity, when the code is in _init.php, nothing, when it is in tarifs.php the table disappears.

Perhaps it comes from settings in ProcessWire...(?), for these fields.

I'm starting to think it's better to give up (for now). We are wasting too much time for this detail (even though it would be nicer)...

I'd like to understand why setlocale didn't work for thousands.

Edit: thanks anyway for your help @kixe!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...