Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add decimal to number string

Status
Not open for further replies.

FireGeek21

Technical User
Dec 12, 2007
218
US
New to JS and appreciate the help on this forum!

Have numbers as stings whereby the last 2 numbers were understood as the decimal. This needs to change. Would like help on how to do this.

String = 123456

Need = 1234.56

FireGeek
(currently using Crystal Reports XI with Lawson 9.01)
 
Hi

Honestly, [tt]String.substring()[/tt] would be among the lasts I would use :
JavaScript:
s [teal]=[/teal] [i][green]'123456'[/green][/i]
s[teal].[/teal][COLOR=orange]slice[/color][teal]([/teal][purple]0[/purple][teal], -[/teal][purple]2[/purple][teal]) +[/teal] [i][green]'.'[/green][/i] [teal]+[/teal] s[teal].[/teal][COLOR=orange]slice[/color][teal](-[/teal][purple]2[/purple][teal])[/teal] [gray]// accepts negative values for both beginIndex ( 1[sup]st[/sup] param ) and endIndex ( 2[sup]nd[/sup] param )[/gray]
s[teal].[/teal][COLOR=orange]substr[/color][teal]([/teal][purple]0[/purple][teal],[/teal] s[teal].[/teal]length [teal]-[/teal] [purple]2[/purple][teal]) +[/teal] [i][green]'.'[/green][/i] [teal]+[/teal] s[teal].[/teal][COLOR=orange]substr[/color][teal](-[/teal][purple]2[/purple][teal])[/teal] [gray]// accepts negative value only for length ( 2[sup]nd[/sup] param ), but not for start ( 1[sup]st[/sup] param )[/gray]
s[teal].[/teal][COLOR=orange]substring[/color][teal]([/teal][purple]0[/purple][teal],[/teal] s[teal].[/teal]length [teal]-[/teal] [purple]2[/purple][teal]) +[/teal] [i][green]'.'[/green][/i] [teal]+[/teal] s[teal].[/teal][COLOR=orange]substring[/color][teal]([/teal]s[teal].[/teal]length [teal]-[/teal] [purple]2[/purple][teal])[/teal] [gray]// not accepts negative values for indexStart ( 1[sup]st[/sup] param ) and indexEnd ( 2[sup]nd[/sup] param ) either[/gray]

s[teal].[/teal][COLOR=orange]replace[/color][teal]([/teal][fuchsia]/..$/[/fuchsia][teal],[/teal] [i][green]'.$&'[/green][/i][teal])[/teal] [gray]// less efficient way[/gray]
[teal]([/teal]s [teal]/[/teal] [purple]100[/purple][teal]).[/teal][COLOR=orange]toFixed[/color][teal]([/teal][purple]2[/purple][teal])[/teal] [gray]// just another way[/gray]


Feherke.
feherke.github.io
 
Without testing and off the top of my head

Converting the string to a number with [parseInt()] then divide the result by 100 to give a two decimal float should work.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top