Coder Social home page Coder Social logo

roman's Introduction

roman

roman-number parse and format

本包提供了罗马数字字符串的解析,和将整数格式化为罗马数字字符串的函数。

func ParseRoman(s string) (uint, error)

函数接受一个字符串,解析并返回一个无符号整数。 如果没有解析到数字,或者解析结束后仍有部分字符数据,会返回一个错误。 (即使返回了错误,返回值也是有意义的,0表示没有解析到数字,否则返回值为解析到的数字)

本函数返回的错误,都是PlaceError类型的。

type PlaceError int

可以用int(err.(PlaceError))来获取整数值,该值表示解析结束的位置。

func FormatRoman(i uint) (string, error)

函数接受一无符号整数,返回对应的罗马数字字符串。 如果i无法表示为罗马数字,会返回一个错误。可表示的范围为(0,4000),不包括0和4000。

事实上,罗马数字表示法可以表示4000及以上的数字,但是要在字符上方划线(不是标准ascii字符)。

因此,本包将罗马数字表示法的字符暴露出来,以方便定制。

var Units = [][3]rune{{'I', 'V', 'X'}, {'X', 'L', 'C'}, {'C', 'D', 'M'}}

切片中每一[3]rune,都表示一个位;每个[3]rune都含有三个字符,依次表示该位的1、5、10; 即是说,I为个位的1,V为个位的5,X为个位的10;X为十位的1,L为十位的5,C为十位的10;依次类推。 用户可以对该变量进行操作,以便扩罗马数字表示的范围。

roman's People

Contributors

hydra13142 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.