amuz.es/src/go/misc@v1.0.1/han/humanize.go (about)

     1  package han
     2  
     3  import (
     4  	"amuz.es/src/go/misc/strutil"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	koreanPluralizeSubunit = 10
    10  	koreanPluralizeUnit    = 10000
    11  	zeroChr                = '0'
    12  )
    13  
    14  var (
    15  	koreanPluralizeUnitLabels    = []string{"", "만", "억", "조"}
    16  	koreanPluralizeSubunitLabels = []string{"", "십", "백", "천"}
    17  )
    18  
    19  func transKoreanPluralizeSubunit(number int) (out string) {
    20  	if number == 0 {
    21  		return "0"
    22  	}
    23  	var (
    24  		buf   strings.Builder
    25  		digit int
    26  	)
    27  	for radix := 0; number > 0; radix++ {
    28  		digit, number = number%koreanPluralizeSubunit, number/koreanPluralizeSubunit
    29  		if digit > 0 {
    30  			buf.WriteString(koreanPluralizeSubunitLabels[radix%len(koreanPluralizeSubunitLabels)])
    31  		}
    32  		if digit > 1 || (digit == 1 && radix == 0) {
    33  			buf.WriteByte(zeroChr + byte(digit))
    34  		}
    35  	}
    36  	return buf.String()
    37  }
    38  
    39  func transKoreanPluralizeUnit(number int, subunitmapper func(int) string, omitLastOne bool) (out string) {
    40  	if number == 0 {
    41  		return "0"
    42  	}
    43  	var (
    44  		buf              strings.Builder
    45  		digit, lastDigit int
    46  	)
    47  	for radix := 0; number > 0; radix++ {
    48  		digit = number % koreanPluralizeUnit
    49  		number = number / koreanPluralizeUnit
    50  		lastDigit = digit % koreanPluralizeSubunit
    51  		if digit > 0 {
    52  			buf.WriteString(koreanPluralizeUnitLabels[radix%len(koreanPluralizeUnitLabels)])
    53  		}
    54  		// 1 뭉개기
    55  		if omitLastOne && radix > 0 && lastDigit == 1 {
    56  			digit--
    57  		}
    58  		//나머지 처리
    59  		if digit > 1 || (digit == 1 && (radix == 0 || !omitLastOne)) {
    60  			buf.WriteString(subunitmapper(digit))
    61  		}
    62  	}
    63  	return buf.String()
    64  }
    65  
    66  // KoreanPluralizeSubunit is a function for translating into Korean after omitting the most significant digit when the most significant digit is 1.
    67  func KoreanPluralizeSubunit(number int) (out string) {
    68  	out = strutil.Reverse(transKoreanPluralizeSubunit(number))
    69  	return
    70  }
    71  
    72  // KoreanPluralizeUnit is a function for translating into Korean after omitting the most significant digit when the most significant digit is 1.
    73  func KoreanPluralizeUnit(number int) (out string) {
    74  	out = strutil.Reverse(transKoreanPluralizeUnit(number, transKoreanPluralizeSubunit, true))
    75  	return
    76  }
    77  
    78  // KoreanPluralizeUnitType2 function is a function that translates only each 10,000 units into Korean.
    79  func KoreanPluralizeUnitType2(number int) (out string) {
    80  	out = strutil.Reverse(transKoreanPluralizeUnit(number, strutil.FormatIntToStringReversed, false))
    81  	return
    82  }
    83  
    84  //KoreanPluralizeUnitType3 is a function for translating into Korean.
    85  func KoreanPluralizeUnitType3(number int) (out string) {
    86  	if number == 0 {
    87  		return "0"
    88  	}
    89  	var (
    90  		below10thousand                        = number % koreanPluralizeUnit
    91  		above10thousand                        = (number / koreanPluralizeUnit) * koreanPluralizeUnit
    92  		outBelow10thousand, outAbove10thousand string
    93  	)
    94  
    95  	if below10thousand > 0 {
    96  		outBelow10thousand = strutil.FormatIntToStringReversed(below10thousand)
    97  	}
    98  	if above10thousand > 0 {
    99  		outAbove10thousand = transKoreanPluralizeUnit(above10thousand, transKoreanPluralizeSubunit, false)
   100  	}
   101  	out = strutil.Reverse(outBelow10thousand + outAbove10thousand)
   102  	return
   103  }