NEWS

[C#] Giới thiệu thư viện Humanizer đọc số thành chữ tiếng việt, tiếng anh, la mã

[C#] Giới thiệu thư viện Humanizer đọc số thành chữ tiếng việt, tiếng anh, la mã
Đăng bởi: Thảo Meo - Lượt xem: 5927 08:31:44, 25/09/2020PHẦN MỀM

Xin chào các bạn bài viết hôm nay mình sẻ giới thiệu đến các bạn thư viện Humanizer by Humanizr C#, dùng để xử lý chuỗi, enum, ngày tháng, byte...

[C#] Using Humanizer Convert Number to Words

Humanizer là một thư viện c#, hỗ trợ chúng ta xử lý những vấn đề liên quan đến: Strings, Date, Time, Enum, Timespan, Quantity...

Trong bài viết này mình sẽ demo ứng dụng đọc số thành chữ:

  1. Đọc số thành chữ tiếng anh
  2. Đọc số thành chữ tiếng việt
  3. Chuyển đổi số sang số La mã (Roman)

Dưới đây là giao diện demo ứng dụng đọc số thành chữ c#:

convert_number_to_words

Các bạn cài đặt thư viện từ Nuget Console:

PM> Install-Package Humanizer -Version 2.8.26

Ở phần đọc số thành chữ, thư viện Humanizer hỗ trợ rất nhiều ngôn ngữ: Tiếng Anh, Việt, Pháp, Nga, Brazil...

Ngoài đọc số thành chữ ra thư viện này còn hỗ trợ rất nhiều thứ:

Chuyển đổi chữ sang chữ thường, hoa, chữ hoa đầu dòng...

"Sentence casing".Transform(To.LowerCase) => "sentence casing"
"Sentence casing".Transform(To.SentenceCase) => "Sentence casing"
"Sentence casing".Transform(To.TitleCase) => "Sentence Casing"
"Sentence casing".Transform(To.UpperCase) => "SENTENCE CASING"

Quy đổi byte thành KB, MB, TB, GB

7.Bits().ToString();         // 7 b
8.Bits().ToString();         // 1 B
(.5).Kilobytes().Humanize();   // 512 B
(1000).Kilobytes().ToString(); // 1000 KB
(1024).Kilobytes().Humanize(); // 1 MB
(.5).Gigabytes().Humanize();   // 512 MB
(1024).Gigabytes().ToString(); // 1 TB

Làm việc với datetime

DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"

DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"

DateTimeOffset.UtcNow.AddHours(1).Humanize() => "an hour from now"

Các bạn có thể truy cập vào trang chủ của Humanizer để tìm hiểu chi tiết về thư viện này https://humanizr.net/

Source code đọc số thành chữ (Convert number to words) C#:

using Humanizer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HumanizerDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_convert_Click(object sender, EventArgs e)
        {
            var number = Convert.ToInt32(txt_input.Text);
            txt_output_en.Text = number.ToWords().Transform(To.SentenceCase);
            txt_output_vi.Text = number.ToWords(new System.Globalization.CultureInfo("vi")).Transform(To.SentenceCase);
            try
            {
                txt_Roman.Text = number.ToRoman();
            }
            catch (Exception)
            {                
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: đọc số thành chữ c#number to roman c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Giới thiệu thư viện Humanizer đọc số thành chữ tiếng việt, tiếng anh, la mã
Đăng bởi: Thảo Meo - Lượt xem: 5927 08:31:44, 25/09/2020PHẦN MỀM