pad a string with zeros c++ code example

Example 1: python format string zero pad

>>> n = '4'
>>> print(n.zfill(3))
# 004
>>> n = 4
>>> print(f'{n:03}') # Preferred method, python >= 3.6
#004
>>> print('%03d' % n)
#004
>>> print(format(n, '03')) # python >= 2.6
#004
>>> print('{0:03d}'.format(n))  # python >= 2.6 + python 3
#004
>>> print('{foo:03d}'.format(foo=n))  # python >= 2.6 + python 3
#004
>>> print('{:03d}'.format(n))  # python >= 2.7 + python3
#004

Example 2: c++ float array zero

float arr1[10] = { };       // all elements are 0
float arr2[10] = { 0 };     // all elements are 0
float arr3[10] = { 1 };     // first element is 1, all others are 0
float arr4[10] = { 1, 2 };  // first element is 1, second is 2, all others are 0

Tags:

Cpp Example