Remove some of trailing zeros from decimal - sql

I have column decimal(10,5) — VALUE, and column int — PICTURE.
So, I need to round VALUE to PICTURE digits after decimal point.
For example:
if VALUE = 10.50000 and PICTURE = 2, I want to get 10.50;
if VALUE = 0.15371 and PICTURE = 3, I want to get 0.154
If I use just Round() or Cast(Round(...) as nvarchar), then I have trailing zeros. If I Cast() to float, then I loose zeros.
Is there any solution?

You can use the str() function with trim():
select trim(str(value, 20, picture))
str() converts a number to a string with the specified length and precision. Sadly, it produces a fixed length string, left padded with spaces. The trim() removes the spaces.

Related

How to write in vb.net a decimal number without rounding the 12 digits after the decimal point?

I try to write the result of the division of the number 5991 by 2987 in vb.net, without rounding the decimal or floating point, strictly equal to 2.005691329092 on 12 digits after the decimal point. The decimal part (0.05691329092) is also noted 17/2987 in scientific notation. My tests always led me to 2.005691329093 rounding!
2 17/2987
Actual value is correct, issue is how to format value to display it as expected.
There are no built-in method, but you can multiply value by required amount of digits after decimal point and truncate it.
Dim value As Double = 5991.0 / 2987.0
value = Math.Truncate(value * Math.Pow(10, 12)) / Math.Pow(10, 12)
Console.WriteLine($"Result: {value:F12}")

Remove leading zeroes in Lua string

I have number string of 6 digits but sometimes I become number with leading zeroes, how can I simple and handily remove that leading zeroes in Lua?
You can strip leading zeros with string operations. e.g.:
x = x:match("0*(%d+)")
Done... It was simple...
join = string.format("%u", join);
This one appears to work also for empty strings, string without leading zeros, etc:
x = string.gsub(x, '0*', '', 1)
or
x = x:gsub('0*', '', 1)

Rounding to a Significant Digit, VB

In the vb function 'round', I want to be able to round to one digit after the decimal. It seems like in the following code:
round([SHAPE_Area]/10000, 1) & " ha"
Where I want to round to to one place after the decimal, if the value for that palce after the decimal is 0, then it rounds to a whole number (i.e. 1 instead of 1.0) I would like it to round to 1.0. The [SHAPE_Area] field is of type 'Double'. I'm not sure if that has any significance to the vb rounding function and how it rounds.
The number is rounded correctly. The number 1 is the same thing as the number 1.0.
What you want to do is formatting the number:
Dim formatted As String = String.Format("{0:N1} ha", SHAPE_Area)
Use .ToString() with the correct Format String instead:
([SHAPE_Area]/10000).ToString("F1") & " ha"

double rounded to 1 when using MsgBox(d) and Console.WriteLine(d)

Why vb prints out 1??? when d is a double aproximation to 1? shoudnt be 0.99999 or something similar? what if I really need the float value? and how could I print it?
Dim d As Double
For i = 1 To 10
d = d + 0.1
Next
MsgBox(d)
Console.WriteLine(d)
thanks
When using MsgBox or Console.WriteLine, double.ToString() is called in order to convert the double to a string.
By default this uses the G format specifier.
The general ("G") format specifier converts a number to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present. The precision specifier defines the maximum number of significant digits that can appear in the result string. If the precision specifier is omitted or zero, the type of the number determines the default precision, as indicated in the following table.
And:
However, if the number is a Decimal and the precision specifier is omitted, fixed-point notation is always used and trailing zeros are preserved.
When converting the infinite 0.9999999.... to a string, since it goes forever, rounding occurs, this results in 1.
A simple test is to run this:
MsgBox((0.9999999999999999999999999).ToString())

Getting float=04 out of int=4

I need to convert integers into two decimal float numbers. So if i have an integer 3 i want it to be float 03. But if i have an two digit integer 33, i want it to be float 33.
The float value will always just be the actual number value. If you want to display that value and have to appear with a leading 0, you will have to use string formatting and the format options to produce "03" from a value of 3.