# Nf() issue with decimal point

**URL:** <https://discourse.processing.org/t/nf-issue-with-decimal-point/4125>\
**Category:** Coding Questions\
**Created:** [October 4, 2018, 12:49pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125 "2018-10-04T12:49:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 4, 2018, 12:49pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/1 "2018-10-04T12:49:31Z")

</div>

Hi,

The following prints 1,2 (with comma) instead of 1.2:

```auto
float f = 1.23;
String s = nf(f, 0, 1);
println(s); // Prints "1,2"

```

That behaviour was already [reported](https://forum.processing.org/two/discussion/16206/nf-with-period) in April 2016, yet contiues to contradict the description of [nf()](https://processing.org/reference/nf_.html).

With only one parameter, it works fine, but then str() would also work:

```auto
float f = 1.23;
String s = str(f);
println(s); // Prints "1.23"

```

nfc() is even less perfect:

```auto
float f = 42525.34343; 
String s = nfc(f, 2);
println(s); // Prints "42 525,34 instead of 42,525.34"

```

Thanks

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 4, 2018, 12:57pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/2 "2018-10-04T12:57:04Z")

</div>

Are you using a system that is set to use UK English? That is how they do decimals over there. As the link you have mentioned points out, you can correct this by tacking on:  
`.replace(',', '.').replace(' ', ',');`

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 4, 2018, 1:27pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/3 "2018-10-04T13:27:28Z")

</div>

> [@TfGuy44](#):
>
> Are you using a system that is set to use UK English?

My system uses a decimal point and a space as a digit grouping symbol. Just tried changing the latter to a comma, but nfc() didn’t care.  
Thank you

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [October 4, 2018, 5:22pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/4 "2018-10-04T17:22:42Z")

</div>

@TfGuy44 It is common for commas and decimals to be switched in many European countries or to have spaces instead of commas (in comparison with english-speaking countries).

[https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html](https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html)

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [October 4, 2018, 5:24pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/5 "2018-10-04T17:24:38Z")

</div>

Yes, thank you for restating what I already said back to me. Here is a lemon for you. 🍋

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [October 4, 2018, 7:39pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/6 "2018-10-04T19:39:23Z")

</div>

Oh, right, I misunderstood you. 😛  
Lemon accepted.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 4, 2018, 10:41pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/7 "2018-10-04T22:41:27Z")

</div>

> [@TfGuy44](#):
>
> Yes, thank you for restating what I already said back to me. Here is a lemon for you. 🍋

Actually, didn’t you state this is how it’s done in UK English? Which isn’t true and the exact opposite of what @TheWizardBear said!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 12, 2018, 3:09pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/8 "2018-10-12T15:09:26Z")

</div>

@gyula –

Some forum members suspect that this relates to the language / region settings of your machine. Can you share something about whether your computer keyboard, OS, or user profile are configured to a particular country or language? The more information we have the more likely it is that someone can suggest a workaround or that fixes can eventually be incorporated.

You may wish to search for an existing issue here:

> **[processing/processing](https://github.com/processing/processing/issues/)**
>
> Source code for the Processing Core and Development Environment (PDE) - processing/processing

and if there is not one, report it.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 12, 2018, 3:47pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/9 "2018-10-12T15:47:56Z")

</div>

> [@jeremydouglass](#):
>
> Some forum members suspect that this relates to the language / region settings of your machine.

More the JVM. Can you try running -

```auto
import java.util.Locale;

float f = 1.23;
String s = nf(f, 0, 1);
println(s); // Prints "1,2"
println(Locale.getDefault().getLanguage());
println(Locale.getDefault().getCountry());

```

Also, find where the preferences file is (it’s location is at the bottom of the preferences window at `File | Preferences`. Edit this file _when Processing is not running_ and try changing the run.options line to

```auto
run.options=-Duser.language=en -Duser.country=US

```

Then re-run Processing and the sketch above.

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 15, 2018, 9:00am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/10 "2018-10-15T09:00:07Z")

</div>

@neilcsmith As you suggested, I ran this code,

```auto
import java.util.Locale;

float f = 1.23;
String s = nf(f, 0, 1);
println(s); // Prints "1,2"
println(Locale.getDefault().getLanguage());
println(Locale.getDefault().getCountry());

```

which printed:

```auto
1,2
en
US

```

Then I quit Processing, in the preferences.txt file changed the line from  
`run.options=`  
to  
`run.options=-Duser.language=en -Duser.country=US`

Re-ran Processing and your code, and surprise, surprise I got back my decimal point:

```auto
1.2
en
US

```

I tried my original nfc() code, and now it also worked properly:

```auto
float f = 42525.34343; 
String s = nfc(f, 2);
println(s); // Prints correctly "42,525.34"

```

Thanks for the tip!  
Do other people also need to change the `run.options=` line in the preferences file to get nf() produce the correct delimiters?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 15, 2018, 9:06am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/11 "2018-10-15T09:06:16Z")

</div>

Did you really get the output `en / US` before making the change?! 🧐 That’s really strange if so.

What I suggested is just a workaround to force the locale settings - I’d report a bug on this, although whether the documentation or code should be updated is another question.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 15, 2018, 9:26am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/12 "2018-10-15T09:26:37Z")

</div>

you see that too?

```auto
float f = 654321.23;
println(" nf "+nf(f, 0, 1)); // Prints " nf 654321.2"
println(" nfc "+nfc(f, 2)); // Prints " nfc 654,321.25"

```

the last decimale “5” looks wrong?

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 15, 2018, 10:31am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/13 "2018-10-15T10:31:27Z")

</div>

> [@neilcsmith](#):
>
> Did you really get the output `en / US` before making the change?! 🧐 That’s really strange if so.

I did, despite [my Win7 system settings](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/3):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5a7701430ae7e4fe9b73b0db692efa6b9f898926.png)

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 15, 2018, 10:38am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/14 "2018-10-15T10:38:47Z")

</div>

I’m not sure how much the Windows settings affect the JDK Locale configuration. However, I’m really surprised that setting the locale language and country to _what they claim to be before_ changes anything! I was expecting the output to be something else.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 15, 2018, 10:46am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/15 "2018-10-15T10:46:51Z")

</div>

One more script you could try with your old / clean settings (which hopefully might show something unexpected) -

```auto
import java.util.Locale;

float f = 1.23;
String s = nf(f, 0, 1);
println(s); // Prints "1,2"
println(Locale.getDefault());
println(Locale.getDefault(Locale.Category.DISPLAY));
println(Locale.getDefault(Locale.Category.FORMAT));

```

It is the value of the last line that _should_ be affecting the output.

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 15, 2018, 11:05am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/16 "2018-10-15T11:05:00Z")

</div>

> [@neilcsmith](#):
>
> One more script you could try with your old / clean settings (which hopefully might show something unexpected) -
> 
> ```auto
> import java.util.Locale;
> 
> float f = 1.23;
> String s = nf(f, 0, 1);
> println(s); // Prints "1,2"
> println(Locale.getDefault());
> println(Locale.getDefault(Locale.Category.DISPLAY));
> println(Locale.getDefault(Locale.Category.FORMAT));
> 
> ```
> 
> It is the value of the last line that _should_ be affecting the output.

I ran your code with my old Processing preferences, that is without this line:  
`run.options=-Duser.language=en -Duser.country=US`

I got:

```auto
1,2
en_US
en_US
hu_HU

```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 15, 2018, 11:15am UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/17 "2018-10-15T11:15:52Z")

</div>

OK, that makes more sense. So your system is somehow registering that it should use Hungarian for formatting things like numbers, dates, etc. That might be related to a setting in Windows somewhere, but I’ve no idea where! Or why they’re different.

It might be worth opening a GitHub issue on this. Firstly to work out whether the docs are wrong or not - is the intention to use the locale settings for format. And, secondly, to document where those settings are read from / whether Processing should use something other than the default mechanism for finding them. Seems all related to changes in [https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html](https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html)

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 15, 2018, 1:56pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/18 "2018-10-15T13:56:54Z")

</div>

> [@kll](#):
>
> ```auto
> float f = 654321.23;
> println(" nf "+nf(f, 0, 1)); // Prints " nf 654321.2"
> println(" nfc "+nfc(f, 2)); // Prints " nfc 654,321.25"
> 
> ```
> 
> the last decimale “5” looks wrong?

Try this without nf() or nfc():

```auto
float f = 654321.23;
println(f); // Prints "654321.25" instead of "654321.23"

```

Single-precision floating-point numbers are usually not accurate above 6 significant digits:  
[Online converter for IEEE 754 numbers with single precision](http://www.binaryconvert.com/result_float.html?decimal=054053052051050049046050051)

---

<div class="post-metadata">

**Author:** ![gyula](https://avatars.discourse-cdn.com/v4/letter/g/ccd318/32.png) [@gyula](https://discourse.processing.org/u/gyula)\
**Post date:** [October 15, 2018, 2:04pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/19 "2018-10-15T14:04:07Z")

</div>

Strangely, println() is not so picky with my old Processing preferences:

```auto
float f = 654321.23;
println(f); // Prints "654321.23"

String s = nf(f, 0, 2);
println(s); // Prints "654321,23"

```

Anyway, thanks a lot!

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [October 15, 2018, 2:29pm UTC](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125/20 "2018-10-15T14:29:33Z")

</div>

> [@gyula](#):
>
> Strangely, println() is not so picky with my old Processing preferences:

Why’s that strange? It’s bypassing the formatting for your locale, which the other functions are documented to do.

[Next page](https://discourse.processing.org/t/nf-issue-with-decimal-point/4125.md?page=2)
