-3

I have a javascript function which takes in Strings of varying length that contains numbers that may or may not begin with a zero or end with a zero after a decimal point.

'01234' is being changed to a number 1234
'123.40' is being changed to a number 123.4

i need to preserve the actual string value

 function muteChannel(channelId) 
 {
     alert("channelId:" + channelId);
     return false;
 }
blitzeus
  • 485
  • 2
  • 10
  • 28
  • 3
    Numbers are just numeric values; they don't have any notion of trailing zeros. If you care about that, use strings. – SLaks Apr 27 '18 at 16:17
  • The exact decimal value of 1.10 is 1.1, however if you want to preserve an exact number of decimal places you can use `toFixed()` i.e: `alert("channelId:" + channelId.toFixed(2));` but that would then always show 2 decimal places only and not 3 or 4 if needed. – Nope Apr 27 '18 at 16:17
  • 3
    Possible duplicate of [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – Pintang Apr 27 '18 at 16:18
  • 3
    *"How would I preserve the exact decimal value?"* `12345` **is** the exact decimal value `12345.0` which **is** the exact decimal value `12345.00` (and so on). See @SLaks's comment: Numbers are just numbers. – T.J. Crowder Apr 27 '18 at 16:19

3 Answers3

1

It sounds like channelId is a number rather than a string. If you try:

alert("12345.0"); // a string

you'll get 12345.0, whereas if you try:

alert(12345.0); // a number

it'll remove the trailing zero. If that's the case, it looks like channelId doesn't have the zero at the end by the time you pass it to this function. If you're sure this value is a string at some point in your code you might be able to find where it's being converted and fix that, but it's hard to say without knowing more.

Hope that helps!

DrRelling
  • 122
  • 1
  • 4
  • 10
1

i was passing my string value in as a number initially. My code initially looked like this

<p:commandLink 
    id="MuteButton"
    type="button"
    value="Unmute" 
    process="@this"
    update="@form" 
    onclick="muteChannel(#{channel.channelId});"
    rendered="#{channel.muted}" 
    disabled="#{channel.status eq 'Hangup' or 
                channel.status eq 'Holding' or 
                channel.status eq 'Unmuting' or 
                channel.status eq 'Dropping'}"/>


but now i put single quotes around the #{channel.channelId} to keep them as strings

<p:commandLink 
        id="MuteButton"
        type="button"
        value="Unmute" 
        process="@this"
        update="@form" 
        onclick="muteChannel('#{channel.channelId}');"
        rendered="#{channel.muted}" 
        disabled="#{channel.status eq 'Hangup' or 
                    channel.status eq 'Holding' or 
                    channel.status eq 'Unmuting' or 
                    channel.status eq 'Dropping'}"/>
blitzeus
  • 485
  • 2
  • 10
  • 28
0

You can use toFixed and tell it how many decimal places you want to keep shown in snippet:

var x = 12345.10
var y = 12345.10000
console.log(x.toFixed(2))
console.log(y.toFixed(5))
Scath
  • 3,777
  • 10
  • 29
  • 40