Skip to main content

Posts

Showing posts from March, 2016

Python: Formatting Large Integer

Hello again. Hehe. This is an equivalency method in Python of earlier JavaScript post about Formatting Large Integer . It has demos of the method (in JavaScript). So before we start, the number format in any application, usually, needs to be readable (for interface, such as the dilly we look at) and process-able -- the original format for calculation and then it will be displayed on the dilly we look at. This is a method to make a large integer readable, of some sort. In another words, this is a Python snippet to insert separators between digits. There are countries which use comma as large digit separator and point as decimal mark, and there are countries that use the opposite of that. The first concept, for example: 1,000,400 (large integer) -- it's one million and four hundred 23.58 (decimal) -- twenty three point fifty eight 1,000,000.253 (seriously) -- one million point two hundred and fifty three And the second one: 1.000.400 (large integer) -- one million and...

JavaScript (and Python): Mapping Array/List of Numbers to Strings and the Other Way Round

1. List of Numbers to List of Strings For example, we have this object: [0, 1, 2, 3, 4, 5] In JavaScript, it's called Array and in Python it's a List . And for some reason, we need to convert every member into String . As you can see above, every member is a number (integer). A. JavaScript We can use the new method map() to do that. Basic looping method using for , while , and do - while can also do the task. But of course, because map() is designed to be sweeter than the basic methods, let's use it. var a = [0, 1, 2, 3, 4, 5]; var b = a.map(function (v) { return v.toString(); }); And BYAM! Variable b now consists of all members of variable a , but each of them is now a string . Use your browser console, and copy paste the code above. And then put console.log(b); to see its content. B. Python In Python, when we design some structure which needs the conversion for some other process, we can use this shorthand: map(str, the_list) As in:...