Wiki

Actions
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Deal with floating-point numbers in WRT environment

Compatibility
Device(s): WRT 1.0

Article
Created: vphong (03 Dec 2007)
Last edited: hamishwillee (10 Oct 2012)

Description

The format of a floating-point number is system language dependent. That is in some languages the radix point symbol is a dot "." and in some languages the radix point symbol is a comma ",". However, the JavaScript engine in WRT never recognizes a comma "," as a radix point of a floating-point number. It does not neither recognize a dot "." as a radix point of a floating-point number if the current system language uses a comma "," as the radix point symbol. Therefore, a defined floating-point number with a dot "." separating the integral part from the fractional part of that number will not always be recognized as a floating-point number. Instead, in those system languages that use a comma as the radix point symbol, the fractional part will be truncated and a floating-point number will become an integer.

E.g. var pi = 3.1416;

```  pi is 3 in Italian or Finnish system language
pi is 3.1416 in English system language
```

Solution:

To make your codes work in all system languages, use the following solutions to solve the problem:

Solution 1: Let the system create the floating-point number

E.g.

```    var pi = 3 + 1416/10000;
var d = 5 + 8/10;
var cir = d * pi;
```

Solution 2: Define a floating-point number as a string, then use the parseFloat() method to convert it to a floating-point number

E.g.

```    var pi = "3.1416";
var d = "5.8";
var cir = parseFloat(d) * parseFloat(pi);
```
73 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.