alisinabh.com

defmodule Alisinabh.Blog do

def handling_non-standard_utf-8_digits_in_elixir_like_a_boss! do # UTC Pub Date: 6/2/2017 1:44

Hey everyone.

Recently i was so busy with my job and college so i had no time for writing here. I apologize to you.

In one of the projects we’ve needed an http service for some kind of software activation.

Although we did’t have a decent time for that project i’ve decided to build that service using phoenix framework. And i decided to make it open sourced!

Project was a nice experiment for me on API’s using elixir and phoenix in production. However i came to a simple problem that i could handle more easily with .NET

The problem was that some users entered their license number in the app using their native language keyboard and as you know, there are lots of different numbers in utf-8 table. In .NET you can simple resolve this issue using char.GetNumericValue, but in elixir i didn’t found any reasonable easy solutions for that. So i’ve decided to make a micro library for that matter.

I’ve named it ‘Numero’ which means Number in Spanish (I don’t know Spanish at all!)

With numero you can normalize number chars in a string or parse a string to number (Integer or Float). Numero is also smart on number types. if your number has fraction points in its string Numero will return you a float, otherwise it will hand you your number as Integer.

Usage Example

result = Numero.normalize("1۲۳۰4a۳tس")
# result = "12304a3tس"

result = Numero.normalize_as_number("1۲۳۰4۳") # Strings without fraction points return Integer
# result = {:ok, 123043}

result = Numero.normalize_as_number("1۲۳۰4۳.۴5") # Strings with fraction points return Float
# result = {:ok, 123043.45}

result = Numero.normalize_as_number!("1۲۳۰4۳.۴5") # Return number as result
# result = 123043.45

Installation

You can visit numero here: https://github.com/alisinabh/Numero (FORK ME!)

Or install it in your projects with:

def deps do
  [{:numero, "~> 0.1.2"}]
end

Thank you for reading this.

end

end

defmodule Alisinabh.Contact do

end