CSS decimal-leading-zero not working for large numbers
There is no list-style
for three digit decimals.
However, you can use pseudo-selectors and counters to achieve what you want here.
Refer code:
ol {
counter-reset: items;
}
li {
display: block;
counter-increment: items;
}
li:before {
content: "00" counter(items)". ";
}
li:nth-child(n+10):before {
content: "0" counter(items)". ";
}
li:nth-child(n+100):before {
content: counter(items)". ";
}
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
<li>Item 7</li>
<li>Item 8</li>
<li>Item 9</li>
<li>Item 10</li>
<li>Item 11</li>
<li>Item 12</li>
<li>Item 13</li>
<li>Item 14</li>
<li>Item 15</li>
<li>Item 16</li>
<li>Item 17</li>
<li>Item 18</li>
<li>Item 19</li>
<li>Item 20</li>
<li>Item 21</li>
<li>Item 22</li>
<li>Item 23</li>
<li>Item 24</li>
<li>Item 25</li>
<li>Item 26</li>
<li>Item 27</li>
<li>Item 28</li>
<li>Item 29</li>
<li>Item 30</li>
<li>Item 31</li>
<li>Item 32</li>
<li>Item 33</li>
<li>Item 34</li>
<li>Item 35</li>
<li>Item 36</li>
<li>Item 37</li>
<li>Item 38</li>
<li>Item 39</li>
<li>Item 40</li>
<li>Item 41</li>
<li>Item 42</li>
<li>Item 43</li>
<li>Item 44</li>
<li>Item 45</li>
<li>Item 46</li>
<li>Item 47</li>
<li>Item 48</li>
<li>Item 49</li>
<li>Item 50</li>
<li>Item 51</li>
<li>Item 52</li>
<li>Item 53</li>
<li>Item 54</li>
<li>Item 55</li>
<li>Item 56</li>
<li>Item 57</li>
<li>Item 58</li>
<li>Item 59</li>
<li>Item 60</li>
<li>Item 61</li>
<li>Item 62</li>
<li>Item 63</li>
<li>Item 64</li>
<li>Item 65</li>
<li>Item 66</li>
<li>Item 67</li>
<li>Item 68</li>
<li>Item 69</li>
<li>Item 70</li>
<li>Item 71</li>
<li>Item 72</li>
<li>Item 73</li>
<li>Item 74</li>
<li>Item 75</li>
<li>Item 76</li>
<li>Item 77</li>
<li>Item 78</li>
<li>Item 79</li>
<li>Item 80</li>
<li>Item 81</li>
<li>Item 82</li>
<li>Item 83</li>
<li>Item 84</li>
<li>Item 85</li>
<li>Item 86</li>
<li>Item 87</li>
<li>Item 88</li>
<li>Item 89</li>
<li>Item 90</li>
<li>Item 91</li>
<li>Item 92</li>
<li>Item 93</li>
<li>Item 94</li>
<li>Item 95</li>
<li>Item 96</li>
<li>Item 97</li>
<li>Item 98</li>
<li>Item 99</li>
<li>Item 100</li>
<li>Item 101</li>
<li>Item 102</li>
</ol>
adding leading zeros to a list AND assign the modified values back to the variable in the data frame
Guessing at this a bit, but it sounds like you are failing to assign the modified values back to the variable in the data frame.
dd <- data.frame(id=c(1,2,3,10))
dd$id <- sprintf("%03d", dd$id)
str(dd)
## 'data.frame': 4 obs. of 1 variable:
## $ id: chr "001" "002" "003" "010"
- I used
dd
rather thandata
for the name of the data frame;data
is also the name of a built-in function in R, and it's best practice to avoid it
Display number with leading zeros
In Python 2 (and Python 3) you can do:
number = 1
print("%02d" % (number,))
Basically % is like printf
or sprintf
(see docs).
For Python 3.+, the same behavior can also be achieved with format
:
number = 1
print("{:02d}".format(number))
For Python 3.6+ the same behavior can be achieved with f-strings:
number = 1
print(f"{number:02d}")
Add leading zeroes to number in Java?
String.format (https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax)
In your case it will be:
String formatted = String.format("%03d", num);
- 0 - to pad with zeros
- 3 - to set width to 3
How to add leading zeros?
The short version: use formatC
or sprintf
.
The longer version:
There are several functions available for formatting numbers, including adding leading zeroes. Which one is best depends upon what other formatting you want to do.
The example from the question is quite easy since all the values have the same number of digits to begin with, so let's try a harder example of making powers of 10 width 8 too.
anim <- 25499:25504
x <- 10 ^ (0:5)
paste
(and it's variant paste0
) are often the first string manipulation functions that you come across. They aren't really designed for manipulating numbers, but they can be used for that. In the simple case where we always have to prepend a single zero, paste0
is the best solution.
paste0("0", anim)
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
For the case where there are a variable number of digits in the numbers, you have to manually calculate how many zeroes to prepend, which is horrible enough that you should only do it out of morbid curiosity.
str_pad
from stringr
works similarly to paste
, making it more explicit that you want to pad things.
library(stringr)
str_pad(anim, 6, pad = "0")
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
Again, it isn't really designed for use with numbers, so the harder case requires a little thinking about. We ought to just be able to say "pad with zeroes to width 8", but look at this output:
str_pad(x, 8, pad = "0")
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "0001e+05"
You need to set the scientific penalty option so that numbers are always formatted using fixed notation (rather than scientific notation).
library(withr)
with_options(
c(scipen = 999),
str_pad(x, 8, pad = "0")
)
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"
stri_pad
in stringi
works exactly like str_pad
from stringr
.
formatC
is an interface to the C function printf
. Using it requires some knowledge of the arcana of that underlying function (see link). In this case, the important points are the width
argument, format
being "d"
for "integer", and a "0"
flag
for prepending zeroes.
formatC(anim, width = 6, format = "d", flag = "0")
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
formatC(x, width = 8, format = "d", flag = "0")
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"
This is my favourite solution, since it is easy to tinker with changing the width, and the function is powerful enough to make other formatting changes.
sprintf
is an interface to the C function of the same name; like formatC
but with a different syntax.
sprintf("%06d", anim)
## [1] "025499" "025500" "025501" "025502" "025503" "025504"
sprintf("%08d", x)
## [1] "00000001" "00000010" "00000100" "00001000" "00010000" "00100000"
The main advantage of sprintf
is that you can embed formatted numbers inside longer bits of text.
sprintf(
"Animal ID %06d was a %s.",
anim,
sample(c("lion", "tiger"), length(anim), replace = TRUE)
)
## [1] "Animal ID 025499 was a tiger." "Animal ID 025500 was a tiger."
## [3] "Animal ID 025501 was a lion." "Animal ID 025502 was a tiger."
## [5] "Animal ID 025503 was a tiger." "Animal ID 025504 was a lion."
See also goodside's answer.
For completeness it is worth mentioning the other formatting functions that are occasionally useful, but have no method of prepending zeroes.
format
, a generic function for formatting any kind of object, with a method for numbers. It works a little bit like formatC
, but with yet another interface.
prettyNum
is yet another formatting function, mostly for creating manual axis tick labels. It works particularly well for wide ranges of numbers.
The scales
package has several functions such as percent
, date_format
and dollar
for specialist format types.
How to retain leading zeros of int variables?
The concept of leading zeros is a display concept, not a numerical one. You can put an infinite number of leading zeros on a number without changing its value. Since it's not a numeric concept, it's not stored with the number.
You have to decide how many zeros you want when you convert the number to a string. You could keep that number separately if you want.
Best way to format integer as string with leading zeros?
You can use the zfill()
method to pad a string with zeros:
In [3]: str(1).zfill(2)
Out[3]: '01'
How can I pad a value with leading zeros?
Since ECMAScript 2017 we have padStart:
const padded = (.1 + "").padStart(6, "0");
console.log(`-${padded}`);
Related Topics
How to Change the Font-Size of an <Option> Element Within <Select>
Get Input Type=Text to Look Like Type=Password
How to Add Background Image for Input Type="Button"
Possible to Style the CSS3 Resize Function
Chrome: Automatic Shift of Web Elements
Use Fieldset Legend with Bootstrap
Chrome Could Play HTML5 Mp4 Video But HTML5Test Said Chrome Did Not Support Mp4 Video Codec
Html.Dropdownlistfor() with Custom Parameter
How to Use Multiple Itemprops in a Span Tag for Schema.Org Rich Snippets
Are Self-Closing Input Tags Valid in HTML 4
Center Navbar Links Without Brand Pushing It to the Right in Bootstrap 4
Open a Direct File on the Hard Drive from Firefox (File:///)
How to Stop the Browser from Url-Encoding Form Values on Get
Height of Flex Container Not Working Properly in Safari
Html5 Placeholder Disappears on Focus