0

I have searched around and don't think I am asking a duplicated question, if I am, I'll delete immediately.

I have this:

$('#dti').text(result3.toFixed(2));

The question is how do I append this with '%'

I am hardcoding a % sign in now but it shows even if there is no value...

5
  • What do you mean by no value? Is it 0 or null or an empty string? Commented Jan 20, 2012 at 22:20
  • 1
    @ShankarSangoli - A TypeError would be thrown if result3 was anything other than a number, and if it was 0 it would return 0.00, so I don't think it could ever be "empty" really. Commented Jan 20, 2012 at 22:23
  • TJ might not have looked at the error console before posting, there might still be an error there. Commented Jan 20, 2012 at 22:25
  • @JamesAllardice - That is correct. We don't know the possible values of result3 that is why I have asked a question to OP. You can see it up there. Commented Jan 20, 2012 at 22:26
  • @ShankarSangoli - Yeah, I know, my comment was meant more as a complement to yours than an argument against it. The question can't really be answered effectively until the question is cleared up. Commented Jan 20, 2012 at 22:27

2 Answers 2

2
$('#dti').text(result3.toFixed(2) + (result3?"%":""));

If result3 can never be emtpy or null but a value >= 0 and you don't want to show 0% then you can try this

$('#dti').text(result3 ? (result3.toFixed(2) + "%") : "");
Sign up to request clarification or add additional context in comments.

3 Comments

(false).toFixed(2) would throw an error, so the check in the second half of the snippet won't work.
@Douglas - That is why I have asked question to OP in the questions comment section. Based on that we can check the null or other condition
Now we don't know if restult3 is 0 whether OP wants to show % or not.
1

How about:

$('#dti').text(num = result3.toFixed(2) ? num + '%' : '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.