How to pad a number with zeros to a specific length in tcl? - numbers

I need to pad a number with a specific length (say 8) where the input number could be of any length.
Example: If the input number is 123456, I want to pad it as 12345600
If I knew that all input numbers would be of the same length (say 6), I would have done something like
[format "$input_num%02d" 0]
But since the input number can be of varied length, how do I achieve this?
Obviously the below didn't work:
set cur_length [expr[llength[split $input_num ""]]
set padding [expr 8 - $cur_length]
set padded_num [format "$input_num%${padding}d" 0]
Any help is appreciated.
Thanks.

It's really simple, actually:
format %-08s $input_num
A number is also a string, so it can be left-justified like a string.
Documentation: format

Related

How to align numbers vertically? Swift, UIKit

I want to align numbers by digits in table rows. For example:
___123.4
-5 678.9
That is, so that tens are under tens, units under units, a fractional number under a fractional number.
To convert a Number to a String, I use the numberStringFormatter function.
func numberStringFormat(_ number: Double) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.maximumFractionDigits = 1
numberFormatter.groupingSeparator = " "
let result = numberFormatter.string(from: NSNumber(value: number))
return result ?? ""
}
This function sets the fractional format, determines the number of decimal places, and groups the numbers before the decimal point by digits.
But if the number is an integer, without fractions, or a digit after a floating point after rounding it turns out to be 0, then the formatted string looks like this, for example, 123
And then these numbers in the rows of the table are shifted and it turns out like this:
----123
5 678.9
That is, the fractional number on the bottom row is under the integer number on the top row.
In my opinion, I can solve this task if I force the Number to always show 0 after converting to a String.
I tried googling but couldn't find an answer to this question.
Maybe someone has already encountered such situations and can suggest a possible solution, or at least in what direction to move?
Or, perhaps there is some other solution without forcing 0 to be shown, but simply aligning the characters vertically?
Any ideas are welcome. I really appreciate your help.
Update: A greate solution from HangarRash.
minimumFractionDigits = 1

Numeric (not integer or double precision) picture token Clarion+Postgresql

i need numeric mask like this:
100
101.1
102.123
Take maksimum given decimal places but if last digit is 0 trim it.
Something like:
#n-12_`2 but trim right 0 and .
Ex:
x = 102.1230057::double precision
select rtrim(rtrim(round(x::numeric, 2)::text, '0'), '.')::numeric
If you don't need right aligned numbers, you can use #S10 (or the length you want).
#P<#.##P style pictures might help.

format of read file in fortran

What should be the read format of the of following dataset in fortran. there are 6 spaces between column 1 and 2 with no space of column 1 from margin.
1911.01.01 2.42873702403226
1911.01.02 3.5057043827303
1911.01.03 2.73602527398387
1911.01.04 6.07213767208333
1911.01.05 6.94818901068145
1911.01.06 3.66986589769583
1911.01.07 1.94565994542339
1911.01.08 2.39384275272177
1911.01.09 3.05526130775417
1911.01.10 2.69990836499194
1911.01.11 6.08406263835833
1911.01.12 7.34200241064516
1911.01.13 2.42873702403226
1911.01.14 3.5057043827303
1911.01.15 2.73602527398387
1911.01.16 6.07213767208333
1911.01.17 6.94818901068145
1911.01.18 3.66986589769583
1911.01.19 1.94565994542339
1911.01.20 2.39384275272177
1911.01.21 3.05526130775417
1911.01.22 2.69990836499194
1911.01.23 6.08406263835833
1911.01.24 7.34200241064516
1911.01.25 2.42873702403226
1911.01.26 3.5057043827303
1911.01.27 2.73602527398387
1911.01.28 6.07213767208333
1911.01.29 6.94818901068145
1911.01.30 3.66986589769583
1911.01.31 1.94565994542339
I tried.....
format (i4,i2,i2,6x,d9.14)
but it didnt work
thanks in advance
elisa
So try
(i4,a1,i2,a1,i2,6x,d16.14)
I think you've made two mistakes:
Not accounting for the . characters in the dates, I don't think that they will automatically be treated as field separators (as , or spaces would be). Obviously you'll want to ignore them.
In your d edit descriptor the number of decimal digits, that is the number after the . ought to be no greater than the total field width, ie the number before the ..

double datatype+iphone

i have a one application i know The range of a double is **1.7E +/- 308 (15 digits).**but in my application i have to devide text box 's value to 100.0 my code is
double value=[strPrice doubleValue]/100.0;
NSString *stramoount=[#"" stringByAppendingFormat:#"%0.2f",value ];
when i devide 34901234566781212 by 100 it give me 349012345667812.12 but when i type
349012345667812124 and devide by 100 it give me by 100 it give me 3490123456678121.00 which is wrong whether i change datatype or how can i change my code
The number 349012345667812124 has 18 decimal digits. the double format only provides slightly less than 16 decimal digits of precision (the actual number is not an integer because the format's binary digits do not correspont directly to decimal ones). Thus it is completely expected that the last 2 or 3 digits cannot be represented accurately, and it already happens when the literal "349012345667812124" is parsed to the double format, before any calculations happen.
The fact that you get the expected result with the number 34901234566781212 means nothing; it just happens to be close enough to the nearest value the double format can represent.
To avoid this problem, use the NSDecimal or NSDecimalNumber types.
Use
NSDecimalNumber * dec=[[NSDecimalNumber decimalNumberWithString:value.text locale: [NSLocale currentLocale]] decimalNumberByDividingBy:[NSDecimalNumber decimalNumberWithString:#"100" locale:[NSLocale currentLocale]]];
NSLog(#"%#",dec);
instead of Double

How to take one particular number or a range of particular number from a set of number?

I am looking for to take one particular number or range of numbers from a set of number?
Example
A = [-10,-2,-3,-8, 0 ,1, 2, 3, 4 ,5,7, 8, 9, 10, -100];
How can I just take number 5 from the set of above number and
How can I take a range of number for example from -3 to 4 from A.
Please help.
Thanks
I don't know what you are trying to accomplish by this. But you could check each entry of the set and test it it's in the specified range of numbers. The test for a single number could be accomplished by testing each number explicitly or as a special case of range check where the lower and the upper bound are the same number.
looping and testing, no matter what the programming language is, although most programming languages have builtin methods for accomplishing this type of task (so you may want to specify what language are you supposed to use for your homework):
procfun get_element:
index=0
for element in set:
if element is 5 then return (element,index)
increment index
your "5" is in element and at set[index]
getting a range:
procfun getrange:
subset = []
index = 0
for element in set:
if element is -3:
push element in subset
while index < length(set)-1:
push set[index] in subset
if set[index] is 4:
return subset
increment index
#if we met "-3" but we didn't met "4" then there's no such range
return None
#keep searching for a "-3"
increment index
return None
if ran against A, subset would be [-3,-8, 0 ,1, 2, 3, 4]; this is a "first matched, first grabbed" poorman's algorithm. on sorted sets the algorithms can get smarter and faster.