SlideShare a Scribd company logo
1 of 4
Download to read offline
By WOJCIECH KLEPACKI wklepa@gmail.com
At some point in a past, I bumped on the Dynamo problem how to add a prefix “0” to the
sequence of numbers. Basically, what I wanted to achieve seemed quite easy to solve. My idea was to
add “0” to the sequence of numbers from 1 to 1000. I’ve developed the script but wasn’t very happy
with the outcome. The script added “0” to literally all numbers. I realized that instead of adding a
prefix I want to format the numbers in some witty and smart way. Well, the logical background was
based on the assumption that the length of the maximum item in a range converted to the string is 4.
Then every number with length 1 should look like 0001, 0002 … 0009 after formatting, subsequently
number with length 2 should take the form of 0010, 0020 … 0099. The same manner should be applied
to numbers with a length of 3 but numbers with 4 in length should stay as they are.
I want to make it clear that I use this procedure very often in a real-world scenario. It’s a vital
part of many of my Dynamo scripts. Renumbering in sequence doors, windows, rooms or other
elements plays an important part in my coding activities.
The matrix of numbers after formatting should look exactly like the one below:
1 - 0001
2 - 0002
.. - ..
9 - 0009
10 - 0010
20 - 0020
.. - ..
100 - 0100
200 - 0200
.. - ..
999 - 0999
1000 - 1000
Looks easy? That’s exactly what I thought so I rolled my sleeves up and wrote a short procedure in
the Design Script. This my Code Block:
By WOJCIECH KLEPACKI wklepa@gmail.com
The first line (after comments marked in green) converts the sequence of numbers to the list
of strings. It’s not compulsory but I want to keep it just to make the concept clear. The reason for
conversion is simple. You are not allowed to add “0” to the number, you have to convert it to the
string first. This is exactly what this line of code does:
conStr=inNum+"";
It takes the range of numbers and adds blank space “” at the end.
Subsequently, script adds “0” prefix based on the assumption:
addPrf=inNum < 10? "000"+conStr:
inNum < 100? "00"+conStr:
inNum < 1000? "0"+conStr: conStr;
If the number is less than 10, add “000” to number converted to string, if number is less than 100,
add “00”, and if number is less than 1000 leave the value as it is.
The script does the job, however, if the range is different and spans from 1 … 100 or 10 … 10000 it
will not work without tweaking. This is an annoying problem to solve in Design Script.
I’ve started to learn Python recently and I came up with a brilliant solution to use a Python
code to solve the problem once and for all. My friend Miguel gave me a hint to use a string.format
method to effectively sort the thing out. This is a snapshot of the Jupyter notebook:
By WOJCIECH KLEPACKI wklepa@gmail.com
And a ready solution “encapsulated” in a custom node:
It works in a very elegant way, formatting any sequence of numbers. The script after putting
everything into Revit – Dynamo world looks exactly like the one in the snapshot below:
By WOJCIECH KLEPACKI wklepa@gmail.com
It’s not only robust but elegant as well, isn’t it?

More Related Content

What's hot

Malot hector sin familia
Malot hector sin familiaMalot hector sin familia
Malot hector sin familiaRoberto Jaimes
 
Photogrammetry_nec.pdf
Photogrammetry_nec.pdfPhotogrammetry_nec.pdf
Photogrammetry_nec.pdfUjjwalSubedi8
 
Image processing (Signal Processing)
Image processing (Signal Processing)Image processing (Signal Processing)
Image processing (Signal Processing)Muhammad Waqas
 
Al – andalus y los reinos cristianos hasta
Al – andalus y los reinos cristianos hastaAl – andalus y los reinos cristianos hasta
Al – andalus y los reinos cristianos hastaMUZUNKU
 
Traffic control using image processing
Traffic control using image processingTraffic control using image processing
Traffic control using image processingChirag Panchal
 
ROBOTICS & DESIGN (Introduction of Robots) EEN-429
ROBOTICS & DESIGN (Introduction of Robots) EEN-429ROBOTICS & DESIGN (Introduction of Robots) EEN-429
ROBOTICS & DESIGN (Introduction of Robots) EEN-429foyez ahammad
 

What's hot (9)

Malot hector sin familia
Malot hector sin familiaMalot hector sin familia
Malot hector sin familia
 
Photogrammetry_nec.pdf
Photogrammetry_nec.pdfPhotogrammetry_nec.pdf
Photogrammetry_nec.pdf
 
Image processing (Signal Processing)
Image processing (Signal Processing)Image processing (Signal Processing)
Image processing (Signal Processing)
 
Al – andalus y los reinos cristianos hasta
Al – andalus y los reinos cristianos hastaAl – andalus y los reinos cristianos hasta
Al – andalus y los reinos cristianos hasta
 
Al Andalus
Al AndalusAl Andalus
Al Andalus
 
Traffic control using image processing
Traffic control using image processingTraffic control using image processing
Traffic control using image processing
 
ROBOTICS & DESIGN (Introduction of Robots) EEN-429
ROBOTICS & DESIGN (Introduction of Robots) EEN-429ROBOTICS & DESIGN (Introduction of Robots) EEN-429
ROBOTICS & DESIGN (Introduction of Robots) EEN-429
 
Digital image processing
Digital image processingDigital image processing
Digital image processing
 
3DSensing.ppt
3DSensing.ppt3DSensing.ppt
3DSensing.ppt
 

Similar to Format Numbers with Leading Zeros Dynamo Script

Donut chart in Revit with Dynamo
Donut chart in Revit with DynamoDonut chart in Revit with Dynamo
Donut chart in Revit with DynamoWojciech Klepacki
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errorsPVS-Studio
 
Lesson4.1 u4 l1 binary representation
Lesson4.1 u4 l1 binary representationLesson4.1 u4 l1 binary representation
Lesson4.1 u4 l1 binary representationLexume1
 
A 64-bit horse that can count
A 64-bit horse that can countA 64-bit horse that can count
A 64-bit horse that can countAndrey Karpov
 
The article is a report about testing of portability of Loki library with 64-...
The article is a report about testing of portability of Loki library with 64-...The article is a report about testing of portability of Loki library with 64-...
The article is a report about testing of portability of Loki library with 64-...PVS-Studio
 
data representation
 data representation data representation
data representationHaroon_007
 
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...SanthoshKumarChandra7
 
Undefined behavior is closer than you think
Undefined behavior is closer than you thinkUndefined behavior is closer than you think
Undefined behavior is closer than you thinkAndrey Karpov
 
binary-numbers-system.doc.docx
binary-numbers-system.doc.docxbinary-numbers-system.doc.docx
binary-numbers-system.doc.docxArvindShukla81
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheetAhmed Elshal
 
Intro to Subnets and Binary Math
Intro to Subnets and Binary MathIntro to Subnets and Binary Math
Intro to Subnets and Binary MathDavid Aspden
 
Create finished floors by room outlines
Create finished floors by room outlinesCreate finished floors by room outlines
Create finished floors by room outlinesWojciech Klepacki
 
Number system in Digital Electronics
Number system in Digital ElectronicsNumber system in Digital Electronics
Number system in Digital ElectronicsJanki Shah
 

Similar to Format Numbers with Leading Zeros Dynamo Script (20)

Donut chart in Revit with Dynamo
Donut chart in Revit with DynamoDonut chart in Revit with Dynamo
Donut chart in Revit with Dynamo
 
An Introduction To Python - Strings & I/O
An Introduction To Python - Strings & I/OAn Introduction To Python - Strings & I/O
An Introduction To Python - Strings & I/O
 
Lesson 24. Phantom errors
Lesson 24. Phantom errorsLesson 24. Phantom errors
Lesson 24. Phantom errors
 
Lesson4.1 u4 l1 binary representation
Lesson4.1 u4 l1 binary representationLesson4.1 u4 l1 binary representation
Lesson4.1 u4 l1 binary representation
 
A 64-bit horse that can count
A 64-bit horse that can countA 64-bit horse that can count
A 64-bit horse that can count
 
The article is a report about testing of portability of Loki library with 64-...
The article is a report about testing of portability of Loki library with 64-...The article is a report about testing of portability of Loki library with 64-...
The article is a report about testing of portability of Loki library with 64-...
 
Number Systems
Number SystemsNumber Systems
Number Systems
 
Mastering Binary For IPV4
Mastering Binary For IPV4Mastering Binary For IPV4
Mastering Binary For IPV4
 
data representation
 data representation data representation
data representation
 
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
50_coding_challengesfffffffffffffffffffffffffffffffffffffffffffffffffffffffff...
 
Undefined behavior is closer than you think
Undefined behavior is closer than you thinkUndefined behavior is closer than you think
Undefined behavior is closer than you think
 
binary-numbers-system.doc.docx
binary-numbers-system.doc.docxbinary-numbers-system.doc.docx
binary-numbers-system.doc.docx
 
I Paddress
I PaddressI Paddress
I Paddress
 
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheet
 
uyuyuy.pdf
uyuyuy.pdfuyuyuy.pdf
uyuyuy.pdf
 
Intro to Subnets and Binary Math
Intro to Subnets and Binary MathIntro to Subnets and Binary Math
Intro to Subnets and Binary Math
 
Create finished floors by room outlines
Create finished floors by room outlinesCreate finished floors by room outlines
Create finished floors by room outlines
 
Number system in Digital Electronics
Number system in Digital ElectronicsNumber system in Digital Electronics
Number system in Digital Electronics
 
Demystifying Machine Learning
Demystifying Machine LearningDemystifying Machine Learning
Demystifying Machine Learning
 
Binary numbers-7-12-2011
Binary numbers-7-12-2011Binary numbers-7-12-2011
Binary numbers-7-12-2011
 

More from Wojciech Klepacki

More from Wojciech Klepacki (10)

Create a camera view by room
Create a camera view by roomCreate a camera view by room
Create a camera view by room
 
Create room separations lines for the structure columns
Create room separations lines for the structure columnsCreate room separations lines for the structure columns
Create room separations lines for the structure columns
 
Controlling the visibility in a Revit family
Controlling the visibility in a Revit familyControlling the visibility in a Revit family
Controlling the visibility in a Revit family
 
Bialystok
BialystokBialystok
Bialystok
 
Sopot
SopotSopot
Sopot
 
Detal rynny
Detal rynnyDetal rynny
Detal rynny
 
BRAWO_portfolio
BRAWO_portfolioBRAWO_portfolio
BRAWO_portfolio
 
ZDZ_portfolio
ZDZ_portfolioZDZ_portfolio
ZDZ_portfolio
 
MROZA_portfolio
MROZA_portfolioMROZA_portfolio
MROZA_portfolio
 
DAMIR_portfolio
DAMIR_portfolioDAMIR_portfolio
DAMIR_portfolio
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 

Recently uploaded (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 

Format Numbers with Leading Zeros Dynamo Script

  • 1. By WOJCIECH KLEPACKI wklepa@gmail.com At some point in a past, I bumped on the Dynamo problem how to add a prefix “0” to the sequence of numbers. Basically, what I wanted to achieve seemed quite easy to solve. My idea was to add “0” to the sequence of numbers from 1 to 1000. I’ve developed the script but wasn’t very happy with the outcome. The script added “0” to literally all numbers. I realized that instead of adding a prefix I want to format the numbers in some witty and smart way. Well, the logical background was based on the assumption that the length of the maximum item in a range converted to the string is 4. Then every number with length 1 should look like 0001, 0002 … 0009 after formatting, subsequently number with length 2 should take the form of 0010, 0020 … 0099. The same manner should be applied to numbers with a length of 3 but numbers with 4 in length should stay as they are. I want to make it clear that I use this procedure very often in a real-world scenario. It’s a vital part of many of my Dynamo scripts. Renumbering in sequence doors, windows, rooms or other elements plays an important part in my coding activities. The matrix of numbers after formatting should look exactly like the one below: 1 - 0001 2 - 0002 .. - .. 9 - 0009 10 - 0010 20 - 0020 .. - .. 100 - 0100 200 - 0200 .. - .. 999 - 0999 1000 - 1000 Looks easy? That’s exactly what I thought so I rolled my sleeves up and wrote a short procedure in the Design Script. This my Code Block:
  • 2. By WOJCIECH KLEPACKI wklepa@gmail.com The first line (after comments marked in green) converts the sequence of numbers to the list of strings. It’s not compulsory but I want to keep it just to make the concept clear. The reason for conversion is simple. You are not allowed to add “0” to the number, you have to convert it to the string first. This is exactly what this line of code does: conStr=inNum+""; It takes the range of numbers and adds blank space “” at the end. Subsequently, script adds “0” prefix based on the assumption: addPrf=inNum < 10? "000"+conStr: inNum < 100? "00"+conStr: inNum < 1000? "0"+conStr: conStr; If the number is less than 10, add “000” to number converted to string, if number is less than 100, add “00”, and if number is less than 1000 leave the value as it is. The script does the job, however, if the range is different and spans from 1 … 100 or 10 … 10000 it will not work without tweaking. This is an annoying problem to solve in Design Script. I’ve started to learn Python recently and I came up with a brilliant solution to use a Python code to solve the problem once and for all. My friend Miguel gave me a hint to use a string.format method to effectively sort the thing out. This is a snapshot of the Jupyter notebook:
  • 3. By WOJCIECH KLEPACKI wklepa@gmail.com And a ready solution “encapsulated” in a custom node: It works in a very elegant way, formatting any sequence of numbers. The script after putting everything into Revit – Dynamo world looks exactly like the one in the snapshot below:
  • 4. By WOJCIECH KLEPACKI wklepa@gmail.com It’s not only robust but elegant as well, isn’t it?