Configure float with decimal in sql or force string to show decimal [duplicate] - sql

This question already has answers here:
Is there a way to cast float as a decimal without rounding and preserving its precision?
(3 answers)
Closed 9 years ago.
I have a table in SQl with a column in float type , this table is used to send values to a one fiscal printer,
code name price
----------------------------------
34 cUP 2,5
36 BOOK 2
37 COMET 1,2
38 TOY 1
IS posible configure SQl to show 1,00 o 2,00 when the value have not cents.
When i send to the printer i use this line :
string preco = vercup.Rows[i]["unitario"].ToString();
how can i force to show 1,00 when the values comes 1.

How you store the data isn't related to how the data is presented. yes, when you present the data you can force it to display two decimals.
select convert(decimal(9,2), price) from table
That's just 1 possible solution.

Related

How to sum column Y for all instances of duplicates in column X in py [duplicate]

This question already has answers here:
How do I Pandas group-by to get sum?
(11 answers)
Closed 1 year ago.
I'm working with data for S&P futures. I have a dataframe of data with every 60min close and the volume traded during each 60min interval. I'd like create a new dataframe to sum up the total volume at each price.
Date
Close
Volume
0
4420
100
1
4420.25
200
2
4420.5
300
3
4420
200
4
4420.75
200
5
4422
300
So for example, for 4420, the total volume would be 300, whereas since there are no duplicates for the rest, their total volume would simply be the volume show.
Sorry if the formatting on this question isn't perfect, new to forums.
Appreciate any help!
Use pandas groupby to perform any type of aggregation groupby
dfg = df.groupby('Close')['Volume'].agg('sum').reset_index()

SQL syntax - division and ceiling functions [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 2 years ago.
I am trying to calculate how many packs of sweats a kid needs based on the amount of individual sweets required. I am trying to figure out the SQL syntax to do this.
I have used ceiling with divide but still not working
select CEILING(NoOfSweets / SweetsPerPack) AS NoOfPacks
Scenarios:
NoOfSweets SweetsPerPack RequiredOutCome NoOfPacks
--------------------------------------------------------
10 10 1 1
5 10 1 0
20 10 2 2
8 10 1 0
7 5 2 1
If the values are integers, then SQL Server does integer division. So, 1/2 is 0 rather than 0.5. I find that the simplest way to get a number with decimal points is to multiply by 1.0:
CEILING(NoOfSweets * 1.0 / SweetsPerPack)

How to change values in a column from object type to float. for Example, "'€220 M" to 220,000,000? [duplicate]

This question already has answers here:
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
(6 answers)
Closed 3 years ago.
I have a column with data as:
1 77M
2 118.5M
3 72M
4 102M
5 93M
6 67M
I need to change this to its numerical value as:
77,000,000
and so on.
I have tried different ways but could not come up with a definite solution.
okay this should work
(df[1].str.replace('M','').astype(float) * 1000000).astype(int).astype(str).apply(lambda x : x[:-6]+','+x[-6:-3]+','+x[-3:])
Output
0 77,000,000
1 118,500,000
2 72,000,000
3 102,000,000
4 93,000,000
5 67,000,000
Name: 1, dtype: object

Converting bytes to GB with exact decimals in SQL Server [duplicate]

This question already has answers here:
Decimal values in SQL for dividing results
(6 answers)
Closed 3 years ago.
The first 2 columns have data in bytes. I am trying to convert it from Bytes to GB. For some reason its not reflecting properly in Free Space Column
select vmi.VMTotalMaxSize, vmi.VMTotalSize,
CONVERT(decimal(10,2),vmi.VMTotalMaxSize/1024/1024/1024) as [VMTotalMaxSize (GB)],
CONVERT(decimal(10,2),vmi.VMTotalSize/1024/1024/1024) as [VMTotalSize (GB)],
CONVERT(decimal(10,2),(vmi.VMTotalMaxSize-vmi.VMTotalSize)/1024/1024/1024) as [VMFreeSpace (GB)]
from tbl_WLC_VMInstance vmi
VM Total Max Size:375809638400, 268435456000, 214748364800
VMTotalSize: 375683809280, 62755176448, 74662805504
VMTotalMaxSize (GB): 350.00, 250.00, 200.00
VMTotalSize (GB):349.00, 58.00, 69.00
VMFreeSpace (GB)0.00, 191.00, 130.00
It would nice to have the data with proper calculation. Thanks
Divide by 1024.0, not 1024 - otherwise, your result is converted to integer thus losing decimal precision.

Convert String into digit in SQL [duplicate]

This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.