Floating point values have diferent decimal separators

Post your questions and problem reports here

Moderator: kfury77

Forum rules
Please try to follow these guidelines. This will help to receive faster and more accurate response.

  • Check the Support section of the corresponding product first. Chances are you will find your answer there;
  • Do not create new topics for already reported problems. Add your comments to the existing topics instead;
  • Create separate topic for each problem request. Do NOT post a number of non-related problem reports in a single topic;
  • Give your topic a meaningful title. Titles such as "A question," "Bug report" and "Help!" provide others no clue what your message is about;
  • Include the version number of the software you are using;
  • This is not an official customer support helpdesk. If you need a prompt and official response, please contact our support team directly instead. It may take a while until you receive a reply in the forum;

Floating point values have diferent decimal separators

Postby pmk65 » Tue May 19, 2015 4:27 pm

Floating point values have different decimal separators depending on OS/Locale.

Im trying to create a plugin for Webuilder which needs to process floating point values, but it turns out to be a real pain.

Example:
Code: Select all
var teststring = _t( 7/3 );
var testfloat = StrToFloat(teststring);
Script.Message(unitMatch);
Script.Message(_t(fl));


On Windows 8.1, both of these gives me this value: 2,33333333333333
Notice the decimal separator used is a COMMA, not a PERIOD.

While on Windows 7, I get the value: 2.33333333333333
Notice the decimal separator used is a PERIOD.

So how to use floating point values in a plugin?

Currently I do something like this:

Code: Select all
       // Create a floating point number and remove the digits to get the decimal seperator.
       var decimalSeparator = RegexReplace(_t(5/2), "\\d*", "", true);


And then replace the decimal seperator before converting string values to floating point.
Code: Select all
StrToFloat(Replace(floatString, ".", decimalSeparator));


But there must be a better way, and force PERIOD to be the decimal separator?

According to the Delphi docs, you can add a 2nd parameter to StrToFloat().
But this parameter (TFormatSettings) is an object, and AFIK it's not possible to create objects in Webuilder plugins.


Program used: WeBuilder 2015 v13.2.0.164
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Floating point values have diferent decimal separators

Postby Aivars » Mon May 25, 2015 3:56 pm

Unfortunately plugin API does not export any functions that can get/set decimal separator value at this moment. But I think your workaround is quite good - a pair of functions: one to convert a decimal with a point to a decimal with the locale separator; other to convert decimal with the locale separator to a decimal with a point.
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2456
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Floating point values have diferent decimal separators

Postby pmk65 » Mon May 25, 2015 8:00 pm

In future versions of the API, it might be an idea to force the decimal separators into a period.
A period is the standard decimal separator in *most* programming languages..
(I bet Im not the only one who find it weird to see code where the decimal is a comma instead of period. :) )
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark


Return to HTMLPad / Rapid CSS / Rapid PHP / WeBuilder Support

Who is online

Users browsing this forum: Google [Bot] and 36 guests

cron