3

I need to get the number after de decimal point. For example I have:

344.567

And I need to get in a variable 567.

I've tried the next:

decimal d=43.3432332343344;

String s = String.valueOf(aa);

String[] arrayDecimal = s.split('.');

But the result is an empty array. How can I do this? Is there any way to get this?

4 Answers 4

6

In addition to the other answer, you can also use String.subStringAfter here as below (there could still be other ways to manipulate the string).

Decimal d = 43.3432332343344;
String s = String.valueOf(d); // assuming you meant to use d here
String afterDecimal = s.substringAfter('.');
1
  • 1
    Thanks. it is exactly what I wanted is easier than I was doing.
    – alejandro
    Commented May 7, 2019 at 16:46
11

You don't need to use String conversion at all to obtain the fractional part of a Decimal.

Decimal d = 5.6;

System.debug(d - d.round(System.RoundingMode.DOWN));

0.6

Decimal d = -5.6;

System.debug(d - d.round(System.RoundingMode.DOWN));

-0.6

1
  • Thank you very much for your answer, but what I wanted is no to get 0.6. I need to asign in a variable: 6. If the number is 560.006 I would like to store 006.
    – alejandro
    Commented May 7, 2019 at 16:39
5

String#split wants a Regular Expression. "." matches "any character." You'll need to "escape" the "." to have it work as you expect:

decimal d=43.3432332343344;
String s = String.valueOf(d);
String[] arrayDecimal = s.split('\\.');
1
  • Thank you very much
    – alejandro
    Commented May 7, 2019 at 16:43
3
Math.abs(d - d.intValue());

d = -7.81 returns 0.81

d - d.intValue();

d = -7.81 returns -0.81

But if you just want the string 81 - the fractional digits, then @sfdcfox answer is the way to go.

2
  • Thank you very much for your answer, but yes, I need to get the String 81 not 0.81.
    – alejandro
    Commented May 7, 2019 at 16:41
  • 1
    @alejandro - be sure to accept sfdcfox's answer pls
    – cropredy
    Commented May 7, 2019 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .