Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do you display a currency in JavaScript

I know in C# you would do something like

decimal balance;
endingBalaceLabel.Text = balance.ToString("c");

but I don't even know if this is possible in JavaScript.

4 Answers

Rich Zimmerman
Rich Zimmerman
24,063 Points

You don't have to define your data types in javascript, they're implied based on how you define them, and you don't have as many like in C/Java (floats, longs, doubles, etc..), numbers are just numbers.

So you can either define a currency as a string or a number

foo = 1.25;
// or 
foo = "$1.25";

Okay thank you. I was also wondering when ever I refresh my page all of my input values are still there. How can I make my page completely reset every time it refreshes?

Tobiasz Gala
seal-mask
.a{fill-rule:evenodd;}techdegree
Tobiasz Gala
Full Stack JavaScript Techdegree Student 23,529 Points

If you are coming from the C# background... there will be a lot of differences especially when you will cover OOP in JS. But anyway, the life is easier here at some point :)

To answer your question... after you refresh page there shouldn't be any values. Only if your script dynamically set them in input fields at startup.

Rich Zimmerman
Rich Zimmerman
24,063 Points

That's just the data being cached. You could probably change your settings with your web browser ( I don't know 100% since i've never looked into it).. or maybe just clear your cache and then refresh, though that'll get tedious.

Rich Zimmerman
Rich Zimmerman
24,063 Points

That’s not wrong, but he would have to explicitly use localStorage for that to be why his data is still being displayed, and if he was I don’t think he’d be asking why it’s happening.

Hey guys thanks for your help. Sorry i should have paste the HTML and JavaScript. HTML

            <div class="all" id="pro8"><!-- Tip, Tax, and Total -->
                <h1>Tip, Tax, and Total</h1>
                <form class="tipTaxTotal">
                        <label for="price">Price:</label>
                        <input type="text" name="price" id="price">
                        <label for="tip">Tip:</label>
                        <input type="text" name="tip" id="tip" readonly>
                        <label for="tax">Tax:</label>
                        <input type="text" name="tax" id="tax" readonly>
                        <label for="total">Total:</label>
                        <input type="text" name="total" id="total" readonly>
                </form>
                <button type="button" class='tipTaxTotalB' id="calculate8">Calculate</button>
                <button type="button" class='tipTaxTotalB' id="clear8">Clear</button>
            </div>

JavaScript

//Tip, Tax, and Total
let priceD = document.querySelector('input[name="price"]');
let tipD = document.querySelector('input[name="tip"]');
let taxD = document.querySelector('input[name="tax"]');
let totalD = document.querySelector('input[name="total"]');
let price, tip, tax, total;
document.addEventListener("click", x => {
    if (x.target.tagName = "BUTTON") switch (x.target.id) {
        case 'calculate8' : price = parseInt(priceD.value); tip = price * .15; tax = price * .07; total = price + tip + tax; 
        tipD.value = tip; taxD.value = tax; totalD.value = total; break;
        case 'clear8' : priceD.value = tipD.value = taxD.value = totalD.value = "";
    }
});