I'm storing numbers such as
2,000
5,000
10,000
in a database with an 'integer column'. When I display them on the app, they're showing up like
2.0
5.0
Is there a column type I can use to make them show up "as is." I tried string before but string doesn't sort well.
Integers should come out like integers. If you try to insert "2,000" internally rails is effectively doing to do a to_i on it which will be 2. (try this on irb "2,000".to_i and "2_000".to_i to see how ruby deals with integer conversions).
Now if you insert 2000 and you get 2000.0 that is not natural or expected for an integer column. I suspect there is some post processing going on on the attribute or that your database is decimal/float column.
Rails Console =>
"2,000".to_i => 2
"2_000".to_i => 2000
helper.number_to_currency(2000) => "$2,000.00"
to fix this, I entered the number in the database without the comma. Then the numbers showed up on the app like 10000.0 and I used number_to_currency to change the number to make it look like a dollar value
In Ruby, you can also enter in integers like this
10_000
5_000
2_000
Related
Following problem. I have decimal numbers in my Google Sheets that I get from a website.
The numbers have 3 digits after the decimal point or they have no comma in there. I need the numbers without a comma and adding Zero after 1,2 => 1200 or 1,22 => 1220 and 2 should be 2.
Say if a number looks like this on the website:
1,200
1,220
1,222
12,220
2
I get the data via Google spreadsheet with the formula =IMPORTDATA("API_LINK")
There is the problem that the zeros that I don't want to lose are automatically deleted by =IMPORTDATA
Means I get that copied into the table like this :
1,2
1,22
1,222
12,22
2
My question is, how do I get the numbers to be automatically converted with another formula or even with a script that looks like this:
1,2 => 1200
1,22 => 1220
1,222 => 1222
12,22 => 12220
2 => 2
So normal numbers should be normal and decimals should be converted.
The formula what I tried but is only working for 1,222 => 1222. The other looke like this 1,2 => 43862 and 1,22 => 1,22 also 12,22 => 12,22.
=IFERROR(VALUE(SUBSTITUTE(A2;",";"."));VALUE(SUBSTITUTE(A2;".";",")))
I tried even to change the settings in the sheet but isn't working at all.
It would be very nice if someone could help me with that.
If your import is removing final zeros, this indicates that what winds up in your sheet is most likely text and not a number at all.
It is always easier to help if you share a sample sheet with full editing permissions. However, given what I know from your post, try this:
=IF(A2="","",VALUE(A2&IFERROR(REPT("0",3-(LEN(A2)-FIND(",",A2))))))
This would also work as an array formula with slight modification (i.e., it could "solve" a whole column with one formula rather than dragging a formula down). For instance, if your raw data were in A2:A, you could put the following array formula in B2 (assuming column B is completely blank below B2):
=ArrayFormula(IF(A2:A="","",VALUE(A2:A&IFERROR(REPT("0",3-(LEN(A2:A)-FIND(",",A2:A)))))))
try:
=ARRAYFORMULA(SUBSTITUTE(TO_TEXT(IMPORTDATA("API_LINK")), ",", ))
You also can use format patterns.
You can define how many digits wants in the decimal side with TEXT function. In this case, always three digits even zeros:
##.000
Then, once you have the number you want, you can remove the decimal separator with the SUBSTITUTE funtion.
Finally you got:
SUBSTITUTE(TEXT(YOURNUMBER;"##.000");",";"")
More info in this google site.
I am facing an issue with Ruby BigDecimal on my Rails 4 app.
I have a "price" attribute in a Thing model, which class is BigDecimal. Whenever I create or update a Thing, I would like to save the "price" attribute with strictly two decimal digits (e.g. "0.00", "10.00", "10.10", "10.01").
But it saves, by default, only one decimal digit if two are not necessary. Here what I insert and what it saved currently :
"0" => "0.0" (instead of "0.00")
"10" => "10.0" (instead of "10.00")
"10.1" => "10.1" (instead of "10.10")
"10.01" => "10.01" (OK)
I don't just want to display the "price" attribute with two decimals (which can be done using "%.2f" % price), I want to save the "price" attribute with two decimal digits in my database. Any way to do so ?
Thanks a lot for your help.
What you are describing seems to me to be purely for display purposes. I don't know of any way to save a decimal to a DB with a specific amount of decimals that don't actually add precision unless you save them as a string.
Seems to me you would be best off creating a method on the Thing model that will return price formatted how you want.
I have a rails app with a basic postgres db, but I realized that some of my columns are strings and it'd be best if they were floats. I'm trying to convert columns for latitude and longitude from varchar to floating-point.
I've tried this post Rails - gmaps4rails gem on postgres but I kept getting this error, ERROR: invalid input syntax for type double precision: "". I'm willing to try anything else, and I've seen solutions for ways to do it with postgres queries, but I'm uncertain of how to implement them. It's a straightforward problem; "-73.88537758790638" I want to become -73.88537758790638. I just can't seem to find a working solution or one that I understand how to implement.
Empty strings cannot be converted to a number for obvious reasons.
You have to account for that. Replace all occurrences with NULL or 0 or something compatible.
For the number of fractional digits in your example you want the data type numeric, not float - neither real (float4) nor double precision (float8). Those are lossy types and not exact enough. See:
Fetch records that are non zero after the decimal point in PostgreSQL
Try for yourself:
SELECT '-73.88537758790638'::real AS _float4
,'-73.88537758790638'::double precision AS _float8
,'-73.88537758790638'::numeric AS _numeric;
Result (up to Postgres 11):
_float4 | _float8 | _numeric
---------+-------------------+-------------------
-73.8854 | -73.8853775879064 | -73.88537758790638
db<>fiddle here
Display improved in Postgres 12 (more extra_float_digits by default):
db<>fiddle here
Numeric types in the manual.
Solution
Single SQL statement (replacing empty strings with NULL):
ALTER TABLE tbl
ALTER COLUMN col1 TYPE numeric USING NULLIF(col1, '')::numeric;
This is a really simple thing to do in rails using a native ORM approach:
change_column :restaurants, :column_name, ‘double precision USING CAST(column_name AS double precision)'
I'm new to using Sphinx and the Thinking Sphinx gem. I have a column that has up to 6 digit integers in it, when I do a sort on that column I'm only getting up to 5 digits by default
and when it is ordering them it is doing it by only the first digit so it looks like this
99999
99998
99997
...
89999
...
79999
...
100999 <- Higher 6 digit number showing up here
10999
10998
even though I have numbers higher than 99999 this so it should look like this
100999
100998
100997
...
...
99999
99998
any help is much appreciated
Make sure that you store integers as integers in your DB.
If it's in a string, no one will complain and everything'll work fine until you want to sort them :)
I have a db column for upc codes setup as a 'numeric' data type in postgres.
I generated scaffolding to input values into this table, but rails is converting the input in FLOAT for some reason,
eg, a 13 digit number entry 1234567890000 is converted to 1234567890000.0
from the logs:
INSERT INTO "upc_codes" ("upccode") VALUES (1234567890000.0) RETURNING "id"
Where is the data type for the SQL statement being set, or not set as the case may be?
What data type are using for this column? Try changing the column type to an integer in a migration:
change_column :upc_codes, :upc_code, :integer
max integer value for an mysql integer should be 2147483647 .
i will suppose this could cause an errors somewhere .
try to change the coulm into a bigint .
aniway ,from my experience is better to handle bignum using string in rails (in this manner you could saftly change db). you could always use to_i later in your code .
sorry for my english.
When you set the column type to NUMERIC are you specifying the precision/scale like this: NUMERIC(13,0)? (13 is precision, 0 is scale)
I submitted this again as an answer because I guess I commented when I should have answered.