Show a leading zero if a number is less than 10
There's no built-in JavaScript function to do this, but you can write your own fairly easily:
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}
EDIT:
Meanwhile there is a native JS function that does that. See String#padStart
console.log(String(5).padStart(2, '0'));
less than 10 add 0 to number
You can always do
('0' + deg).slice(-2)
See slice()
:
You can also use negative numbers to select from the end of an array
Hence
('0' + 11).slice(-2) // '11'
('0' + 4).slice(-2) // '04'
For ease of access, you could of course extract it to a function, or even extend Number
with it:
Number.prototype.pad = function(n) {
return new Array(n).join('0').slice((n || 2) * -1) + this;
}
Which will allow you to write:
c += deg.pad() + '° '; // "04° "
The above function pad
accepts an argument specifying the length of the desired string. If no such argument is used, it defaults to 2. You could write:
deg.pad(4) // "0045"
Note the obvious drawback that the value of n
cannot be higher than 11, as the string of 0's is currently just 10 characters long. This could of course be given a technical solution, but I did not want to introduce complexity in such a simple function. (Should you elect to, see alex's answer for an excellent approach to that).
Note also that you would not be able to write 2.pad()
. It only works with variables. But then, if it's not a variable, you'll always know beforehand how many digits the number consists of.
How to concatenate with a leading zero in number less than 10
Try this:
=A1&"."&TEXT(B1,"00")
Or this also works:
=TEXT(TIME(A1,B1,0),"h.mm")
As required:
adding leading zero if number is < 10
You can use the slice method
diff.sec = tmp % 60;
if( diff.sec < 10 ){
diff.sec = ("0" + diff.sec).slice(-2);
}
JSFiddle with sample value
var test = 9;if( test < 10 ){ test = ("0" + test).slice(-2);}console.log(test);
Adding a 0 if number is less than 10
Your problem is i
is still an integer, it needs to be assigned to a string
for (int i = 1; i <= 36; i++)
{
var iString = i.ToString();
if(iString.Length == 1)
{
iString = iString.PadLeft(2,'0'); //RIGHT HERE!!!
}
Response.Write("Test: " + iString);
}
However, much of this code is superflous, the if
statement is not needed. Pad will only ped with zeroes up to the length (2) given. If it's already 2 or more characters long, it won't pad anything. All you need is this
for (int i = 1; i <= 36; i++)
{
var iString = i.ToString().PadLeft(2,'0');
Response.Write("Test: " + iString);
}
For that matter, the variable is no longer needed.
for (int i = 1; i <= 36; i++)
{
Response.Write("Test: " + i.ToString().PadLeft(2,'0'));
}
And if you'll be padding with zeroes all the time, and not some other character, you could just do this
for (int i = 1; i <= 36; i++)
{
Response.Write("Test: " + i.ToString("00"));
}
And you should get into the habit of using string.Format
for (int i = 1; i <= 36; i++)
{
Response.Write(string.Format("Test: {0}", i.ToString("00")));
}
And to simplify the string.Format
even further:
for (int i = 1; i <= 36; i++)
{
Response.Write(string.Format("Test: {0:00}", i));
}
R-- Add leading zero to string, with no fixed string format
A one-liner using regular expressions:
my_strings <- c("9453", "55489", "4588",
"18893", "4457", "2339", "45489HQ", "7833HQ")
gsub("^([0-9]{1,4})(HQ|$)", "0\\1\\2",my_strings)
[1] "09453" "55489" "04588" "18893"
"04457" "02339" "45489HQ" "07833HQ"
Explanation:
^ start of string
[0-9]{1,4} one to four numbers in a row
(HQ|$) the string "HQ" or the end of the string
Parentheses represent capture groups in order. So 0\\1\\2
means 0
followed by the first capture group [0-9]{1,4}
and the second capture group HQ|$
.
Of course if there is 5 numbers, then the regex isn't matched, so it doesn't change.
Related Topics
How to Make SQL Query Result Show With 2 Decimals
How to Get a List of Column Names on Sqlite3 Database
Checking If a SQL Server Login Already Exists
Sql: Select All Rows If Parameter Is Null, Else Only Select Matching Rows
How to Execute a Stored Procedure Once for Each Row Returned by Query
Access Denied for Load Data Infile in MySQL
How to Select Rows With Only Numeric Characters in Oracle SQL
Sql Server - Join Rows into Comma Separated List
How to Store Output of a SQL Server Stored Procedure in a .Txt File
How to Select All the Columns of a Table Except One Column
How to Compare Timestamp Dates With Date-Only Parameter in MySQL
Select Distinct Values from One Table and Join With Another Table
How to Create Column in SQL Query With Custom Text
Sql Server: Create an Incremental Counter for Records in the Same Year
How to Select Multiple Values in One Field MySQL