How can I display a floating number with no trailing zeros with printf [duplicate] - printf

This question already has answers here:
Avoid trailing zeroes in printf()
(16 answers)
Closed 4 years ago.
I'd like to format floating numbers in such a way that at most 2 decimals are added after the dot.
1.24 should be displayed as 1.24
1.246 should be displayed as 1.25
1.2 should be displayed as 1.2
1.0 should be displayed as 1
Using "%.2f" with printf has a drawback. For numbers with less than 2 decimals after the dot, it adds trailing 0 digits. Therefore, 1.2 is displayed as 1.20 and 1.0 is displayed as 1.00.

you can do a custom print, trim out tailing 0s and then tailing .
e.g. in python
def custom_print(num):
str = ('%.2f' % num).rstrip('0').rstrip('.')
print(str)

Related

Remove all but two trailing zeros AND thousand separator

Postgres Version 11.7
I am trying to remove trailing zeros beyond two decimal places, while also adding thousand separator to my result set.
Example 1 produces the desired result; all trailing zeros were removed:
SELECT to_char(54354.0010, 'FM99 999 999 990.999999'); --> returns 54 354.001
Example 2 illustrates the problem:
SELECT to_char(54354.0000, 'FM99 999 999 990.999999'); --> returns 54 354.
In Example 2 all zeroes after the decimal are removed.
But the desired result would be:
54 354.00
The result should always have a minimum of two decimal places, regardless whether they are zero or not.
This produces your desired result:
SELECT to_char(54354.0000, 'FM99 999 999 990.009999')
Two 0 instead of 9 after the decimal point.
The manual:
9 digit position (can be dropped if insignificant)
0 digit position (will not be dropped, even if insignificant)

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.

SQL Server 2016 format #,### not displaying 0 value

I am calling
FORMAT(myNum, '#,###') AS myNum
Which works for 123456789 as the output is 123,456,789
Also works for negative numbers
However, 0 is showing up as a blank field.
How do I get 0 to show up as 0? I am also curious as to why 0 is being removed as the query without the format shows 0 in that column's field when there should be a 0.
Note: I do not need any decimals and would prefer to use the above code if at all possible.
If you want to display the 0, if it is zero, you should use:
FORMAT(myNum, '#,###0') AS myNum
According to this Reference:
https://msdn.microsoft.com/en-us/library/ee634206.aspx
0 (zero character)
Digit placeholder. Displays a digit or a zero. If the expression has a digit in the position where the zero appears in the format string, displays the digit; otherwise, displays a zero in that position.
If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, displays leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, rounds the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, displays the extra digits without modification.
# Digit placeholder:
Displays a digit or nothing. If the expression has a digit in the position where the # character appears in the format string, displays the digit; otherwise, displays nothing in that position.
This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has fewer digits than there are # characters on either side of the decimal separator in the format expression.

Oracle PL/SQL number to_char ignores 0's even when decimal part exist [duplicate]

This question already has an answer here:
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
Im using the following code to convert a number(38,2) to the format "XXX.XXX.XXX,XX":
TO_CHAR(number, '999G999G999G999G999G999G999D99')
Although, when the number is 0 or 0.XX the 0 is eaten up :
Input: 0
Result: ",00"
Expected Result: "0,00"
Input: 0.23
Result: ",23"
Expected Result: "0,23"
Why does this happen, is there a way of solving this without manipulating the result string ?
Use 0 instead of 9
TO_CHAR(0.00, '999G999G999G999G999G999G990D99')
0 ensures that if there is no digit in that place it'll display 0.

VB.Net About dividing Currency by an X amount of months

im trying to learn how to made stuff with currency.
For example:
I divide 10.000$ by 12 Months, rounding with 2 decimals i have 833,33 $.
If i multiply 833,33 $ * 12 i got 9999,96 $, so there is 0.04 of possible loss.
Rounding the 9999.96 with 2 decimals of presition i got 10.000 $ but that's what i don't want since 0.04 is a loss.
Im using SQL Compact 4.0 as database, the price_month table is decimal(18,2)
Here is my code:
Dim price as Decimal = 10000
Dim pricemonth as Decimal = Math.round((price/12),2) ' 833.33
Console.Writeline(pricemonth*12) ' 9999.96
Console.Writeline(Math.round((pricemonth*12),2)) ' 10000
Any advice how to increase accuracy with currency? Thanks and have a nice day!
Don't round your calculation. Leave the original numbers untouched but when you display the answer round it so that it looks nice.