1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const NUMS: &[char] = &['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];

const POS_001: char = '分';
const POS_01: char = '角';
const POS_0: char = '圆';
const POS_10: char = '拾';
const POS_100: char = '佰';
const POS_1000: char = '仟';

const POS_10K: char = '万';
const POS_1M: char = '亿';

const NUM_EXACTLY: char = '整';

const MINUS: char = '负';

pub trait ChineseCurrency {
    fn to_chinese_currency(self) -> String;
}

impl ChineseCurrency for isize {
    fn to_chinese_currency(self) -> String {
        let (value, is_minus) = if self < 0 {
            (-self as usize, true)
        } else {
            (self as usize, false)
        };

        Builder::new(value, is_minus).build()
    }
}

struct Builder {
    position_groups: PositionGroups,
    pos_01: usize,
    pos_001: usize,
    is_minus: bool,
}

struct PositionGroups {
    position_groups: Vec<PositionGroup>,
}

struct PositionGroup {
    pos_1: usize,
    pos_10: usize,
    pos_100: usize,
    pos_1000: usize,
    rank: Rank,
}

struct Rank {
    rank: u8,
}

impl Builder {
    fn new(mut i: usize, is_minus: bool) -> Builder {
        let pos_001 = i % 10;
        i /= 10;

        let pos_01 = i % 10;
        i /= 10;

        let position_groups = PositionGroups::new(i);

        Builder {
            position_groups,
            pos_01,
            pos_001,
            is_minus,
        }
    }

    fn build(self) -> String {
        let mut buf = String::default();

        if self.is_minus {
            buf.push(MINUS);
        }

        let is_empty = self.position_groups.build(&mut buf);

        let mut exactly = if self.pos_01 > 0 {
            buf.push(NUMS[self.pos_01]);
            buf.push(POS_01);
            false
        } else {
            true
        };

        if self.pos_001 > 0 {
            if exactly {
                buf.push(NUMS[self.pos_01]);
            }

            buf.push(NUMS[self.pos_001]);
            buf.push(POS_001);
            exactly = false;
        }

        if exactly {
            if is_empty {
                buf.push(NUMS[0]);
                buf.push(POS_0);
            }
            buf.push(NUM_EXACTLY);
        }

        buf
    }
}

impl PositionGroups {
    fn new(mut i: usize) -> PositionGroups {
        let mut position_groups = Vec::new();

        let mut rank = 0;
        while i > 0 {
            let pos_1 = i % 10;
            i /= 10;

            let pos_10 = i % 10;
            i /= 10;

            let pos_100 = i % 10;
            i /= 10;

            let pos_1000 = i % 10;
            i /= 10;

            position_groups.push(PositionGroup {
                pos_1,
                pos_10,
                pos_100,
                pos_1000,
                rank: Rank { rank },
            });
            rank += 1;
        }

        PositionGroups { position_groups }
    }

    fn build(&self, buf: &mut String) -> bool {
        if self.position_groups.is_empty() {
            true
        } else {
            let mut group_iter = self.position_groups.iter().rev();
            if let Some(position_group) = group_iter.next() {
                position_group.build(buf, false);
            }

            for position_group in group_iter {
                position_group.build(buf, true);
            }
            buf.push(POS_0);
            false
        }
    }
}

impl PositionGroup {
    fn build(&self, buf: &mut String, show_zero: bool) {
        let mut is_rendered = false;

        if self.pos_1000 > 0 {
            buf.push(NUMS[self.pos_1000]);
            buf.push(POS_1000);
            is_rendered = true;
        } else if show_zero {
            buf.push(NUMS[self.pos_1000]);
        }

        if self.pos_100 > 0 {
            buf.push(NUMS[self.pos_100]);
            buf.push(POS_100);
            is_rendered = true;
        }

        if self.pos_10 > 0 {
            if is_rendered && self.pos_100 == 0 {
                buf.push(NUMS[self.pos_100]);
            }
            buf.push(NUMS[self.pos_10]);
            buf.push(POS_10);
            is_rendered = true;
        }

        if self.pos_1 > 0 {
            if is_rendered && self.pos_10 == 0 {
                buf.push(NUMS[self.pos_10]);
            }
            buf.push(NUMS[self.pos_1]);
            is_rendered = true;
        }

        if is_rendered {
            buf.push_str(&self.rank.to_string());
        } else {
            buf.pop();
        }
    }
}

impl Rank {
    fn to_string(&self) -> String {
        Rank::rank_to_string(self.rank)
    }

    fn rank_to_string(rank: u8) -> String {
        match rank {
            0 => "".into(),
            1 => POS_10K.to_string(),
            _ => {
                let m = rank % 2;
                let d = rank / 2;
                let mut s = String::with_capacity(d as usize + 1);
                if m == 1 {
                    s.push(POS_10K);
                }
                for _ in 0..d {
                    s.push(POS_1M);
                }
                s
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{ChineseCurrency, Rank};

    #[test]
    fn smoke_rank() {
        assert_eq!(Rank::rank_to_string(0), "");
        assert_eq!(Rank::rank_to_string(1), "万");
        assert_eq!(Rank::rank_to_string(2), "亿");
        assert_eq!(Rank::rank_to_string(3), "万亿");
        assert_eq!(Rank::rank_to_string(4), "亿亿");
        assert_eq!(Rank::rank_to_string(5), "万亿亿");
        assert_eq!(Rank::rank_to_string(6), "亿亿亿");
    }

    #[test]
    fn smoke_to_chinese_currency() {
        assert_eq!(0.to_chinese_currency(), "零圆整");
        assert_eq!(1.to_chinese_currency(), "零壹分");
        assert_eq!(21.to_chinese_currency(), "贰角壹分");
        assert_eq!(20.to_chinese_currency(), "贰角");
        assert_eq!(321.to_chinese_currency(), "叁圆贰角壹分");
        assert_eq!(300.to_chinese_currency(), "叁圆整");
        assert_eq!(4321.to_chinese_currency(), "肆拾叁圆贰角壹分");
        assert_eq!(54321.to_chinese_currency(), "伍佰肆拾叁圆贰角壹分");
        assert_eq!(654321.to_chinese_currency(), "陆仟伍佰肆拾叁圆贰角壹分");
        assert_eq!(
            7654321.to_chinese_currency(),
            "柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            87654321.to_chinese_currency(),
            "捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            987654321.to_chinese_currency(),
            "玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            8987654321.to_chinese_currency(),
            "捌仟玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            78987654321.to_chinese_currency(),
            "柒亿捌仟玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            70987654321.to_chinese_currency(),
            "柒亿零玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
        assert_eq!(
            (-70987654321).to_chinese_currency(),
            "负柒亿零玖佰捌拾柒万陆仟伍佰肆拾叁圆贰角壹分"
        );
    }
}