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)
Related
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.
Hi I want to smart round a float number, to keep the number of digits constant.
This number will always have 1 or 2 integer digits + undefined decimal digits.
I want to have a number with always 3 digits regardless of their position
EX:
83.235 = 83.2
0.110321 = 0.11
4.56723 = 4.57
If leading zeroes don't count as digits
NSNumberFormatter has usesSignificantDigits, minimumSignificantDigits, and maximumSignificantDigits properties that should do what you need. Set both values to 3, or just the max if you don't want trailing zeroes on values with fewer decimal places.
If you want leading zeroes to count as digits
I'm not aware of any built-in way of handling this, but you could write some custom logic to do it. e.g.
if (yourNum < 10)
formattedNum = [NSString stringWithFormat:#"%.2f", yourNum];
else
formattedNum = [NSString stringWithFormat:#"%.1f", yourNum];
This is assuming the number is always between 0 and 99.whatever, so if you're not 100% confident in the data integrity you'll probably want some more checks.
I have the following code, which takes a string temp, which for sure has 2 numbers separated by a "(", and then does a couple of mathematical operations on them. I am getting a type mismatch error when trying to do the math, though, despite using CDec to convert them to a decimal number. What can I be doing wrong? Thanks.
tempArry = Split(temp, " ")
tolTemp = (CDec(tempArry(LBound(tempArry)) + tempArry(LBound(tempArry) + 2))) / 2
noms(j) = tolTemp
tols(j) = tolTemp - CDec(tempArry(LBound(tempArry)))
Based on your comments it seems you're assuming you can add two strings as numbers.
Since the input (the temp variable) is a string, the array elements you're trying to add are also strings so when you write tempArry(LBound(tempArry)) + tempArry(LBound(tempArry) + 2) the output will be the concatenation of both strings (which is 112.34117.89 -- hence the type mismatch).
To solve it convert both to decimal before trying to add them:
tolTemp = (CDec(tempArry(LBound(tempArry))) + CDec(tempArry(LBound(tempArry) + 2))) / 2
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"
How can I add a zero to the left of a 1 digit integer ?
Is there any objective C function to perform this?
I am needing this so I can have only one NSDateFormat #"ddMMyyyy" thanks
I want to add a zero to the left of an integer which is less than 10, I don't want to use an if statement.
Is there any function or way to achieve this?
Use the string format specifier %02d and any single digit integer will be padded with a zero in a string.
You can use string formatters to add padding to your integer value as shown here: Help in padding numerical strings