Convert double 15,34 to 15.34 - localization

I'm trying to get a json string but I noticed that when I pass some double numbers to my query the integer part is separated by a coma from the floating part. I need them to be separated by a point. Is this connected to the Language of the os?

You can use a simple regex to fix this issue:
\d+,\d+ to select the offending bits of JSON, and a string-based replace on the results. If you already have the numbers separated out, use the replace function (something like val.replace(",",".")) on the value you have, and cast it to a float (float(val) in Python).

Related

FME change data format from string to numeric

Struggling with a really simple problem; I need to convert attribute from string to numeric in FME. have tried using the arithmetic editor, but every time I export to GIS I get string. It seems when one uses the statistics calculator you get numeric.
Any ideas? As I am all out of them.
Ashton

rails to_json quoting decimal values

(update)
An array of geocoordinates, built from a collection of records
[{"point_lon"=>0.1307336132e3, "point_lat"=>-0.252978933e2, "title"=>"kata tjuta"},
{"point_lon"=>0.154984876e3, "point_lat"=>-0.17e2, "title"=>"error case"},
{"point_lon"=>0.1310369614747e3, "point_lat"=>-0.253455545e2, "title"=>"uluru"}]
has proper quoting structure, but for JSON input to a javascript needs the rockets to be replaced by a colon.
Transforming the array via JSON.generate or to_json unfortunately leads to quoting of the decimal values and being ignored by the javascript
[{"point_lon":"130.7336132","point_lat":"-25.2978933","point_name":"kata tjuta"},
{"point_lon":"154.984876","point_lat":"-17.0","point_name":"error case"},
{"point_lon":"131.0369614747","point_lat":"-25.3455545","point_name":"uluru"}]
How can this array be transformed without quoting decimals?
This is because you use decimal numbers instead of float, so rails quotes the strings to preserve the precision. You can find methods to avoid this in this related question: Rails JSON Serialization of Decimal adds Quotes

Dart - Adding a recurring character to an integer

I'm working with long integers in dart and want to know how to add a comma after every three numbers.
Example;
999999999 would turn into 999,999,999
I get this information from an online API, so I can't change the int from a local file and follow that pattern, I need to intercept the int before/as it's being displayed.
Thanks
After looking at this stack question I found a way to accomplish this if your variable is a string.
This is what that looks like;
import 'package:intl/intl.dart';
final oCcy = new NumberFormat("#,##0.00", "en_US");
"${oCcy.format(yourVar)}",
Despite this, I was actually dealing with an integer, so using this method left me with an error. This was easily fixed by converting the string into an int.
This is what that looks like;
"${oCcy.format(double.parse(yourVar))}",

How do I keep my rails integer from being converted to binary?

As you may be able to see in the image, I have a User model and #user.zip is stored as an integer for validation purposes (ie, so only digits are stored, etc.). I was troubleshooting an error when I discovered that my sample zip code (00100) was automatically being converted to binary, and ending up as the number 64.
Any ideas on how to keep this from happening? I am new to Rails, and it took me a few hours to figure out the cause of this error, as you might imagine :)
I can't imagine any other information would be helpful here, but please inform me if otherwise.
This is not binary, this is octal.
In Ruby, any number starting with 0 will be treated as an octal number. You should check the Ruby number literals to learn more about this, here's a quote:
You can use a special prefix to write numbers in decimal, hexadecimal, octal or binary formats. For decimal numbers use a prefix of 0d, for hexadecimal numbers use a prefix of 0x, for octal numbers use a prefix of 0 or 0o, for binary numbers use a prefix of 0b. The alphabetic component of the number is not case-sensitive.
For your case, you should not store zipcodes as numbers. Not only in the database, but even as variables don't treat them as numeric values. Instead, store and treat them as strings.
The zip should probably be stored as a string since you can't have a valid integer with leading zeroes.

How do I avoid errors when converting strings to numbers if I don't know whether I have floats or integers?

I have stringgrid on delphi form and i am trying to divide values of one cell with value of another cell in another column.
But the problem is, stringgrid cells are populated with different types of numbers, so I am getting ConvertErrors.
For example the numbers in cells can look like
0.37 or 34 or 0.0013 or 0.00 or 0.35 or 30.65 or 45.9108 or 0.0307 or 6854.93.
In another words I never know is it going to be real, float, integer or any other kind of type in those cells.
I have looked everywhere on internet but no luck. Anyone any ideas. By the way I am not exactly Delphi expert. Thanks.
For each string, convert it first to a float value using StrToFloat function in SysUtils.pas . This should allow for any numerical type to be dealt with (unless you have something unusual like complex numbers). As you have some zero values in your list above you should also ensure that you check for divide by zero conditions as this will also potentially throw an exception.
SysUtils has many functions such as TryStrToFloat, TryStrToInt, TryStrToInt64 etc for this purpose. These functions accept a reference parameter (var parameter) for returning the converted value and function itself returns true if the conversion is successful.
If you are sure that the string has a valid number then you can check the input string to see if it has a decimal point before deciding which function to use.
Treat all the numbers as float. Use StrToFloat, divide the numbers, and then convert the result back to string with FloatToStr. If the result is an integer, no decimal point would be produced.

Resources