# How to display the amount of digits in a number?

Page 1 of 1

## 2 Replies - 410 Views - Last Post: 20 January 2013 - 06:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308230&amp;s=bf22597c54d987daeca62c836c4bf151&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Eggy

Reputation: 0
• Posts: 31
• Joined: 15-November 12

# How to display the amount of digits in a number?

Posted 20 January 2013 - 02:47 PM

Ok, say I have

```y = 101.333;
```

How can I display how many digits there are before (3 in this case) and after the decimal point (3 in this case)?

Can we possibly use the DecimalFormat class to do this?

Thank you.

This post has been edited by Eggy: 20 January 2013 - 02:55 PM

Is This A Good Question/Topic? 0

## Replies To: How to display the amount of digits in a number?

### #2 Ryano121

• D.I.C Lover

Reputation: 1376
• Posts: 3,015
• Joined: 30-January 11

## Re: How to display the amount of digits in a number?

Posted 20 January 2013 - 02:52 PM

If it's a double variable -

int digits = Double.toString(y).length;

Same works for Integer, Long, Float etc

Edit - scrap that didn't notice you wanted before and after decimal point.

You could use the split method though.

```String[] parts = Double.toString(y).split("\\.");
int before = parts[0].length;
int after = parts[1].length
```

You should however do a check to see if there is a decimal point in the number to avoid an exception being thrown.

This post has been edited by Ryano121: 20 January 2013 - 02:55 PM

### #3 ianian112

• D.I.C Regular

Reputation: 119
• Posts: 378
• Joined: 28-November 09

## Re: How to display the amount of digits in a number?

Posted 20 January 2013 - 06:18 PM

Ryan's way works but is definitely not the best.
```new BigDecimal(String.valueOf(y)).precision()
```
is probably the easiest way I know.

This post has been edited by ianian112: 20 January 2013 - 06:21 PM