Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2007, 05:14 PM
aceofspades's Avatar
aceofspades aceofspades is offline
 
Join Date: Apr 2006
Posts: 306
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Simple PHP calculation in postbit

Im trying to do a simple calculation in my postbit. Bascially it square roots a vb value and displays the answer to 1 significant figure.

For example if the value is 6 then the real answer would be 2.449... but it would display it as 2. I know pratically nothing about php, so all i have so far is this (which is probably wrong already):

<?php
echo sqrt($input);
?>

Can anyone help me on where i would go from here? It would be much appreciated.

James

EDIT:

Right im a little bit further on thanks to nick from vbnova. To get it to 0 decimal places ( i dont want to round the numbers up / down) ive got this code:

$number = 1.953434;
$number = str_split($number);

$number = $number[0];

But i really dont know how to put these two codes together. Can anyone help me out?
Reply With Quote
  #2  
Old 07-13-2007, 11:55 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by aceofspades View Post
Im trying to do a simple calculation in my postbit. Bascially it square roots a vb value and displays the answer to 1 significant figure.

For example if the value is 6 then the real answer would be 2.449... but it would display it as 2. I know pratically nothing about php, so all i have so far is this (which is probably wrong already):

<?php
echo sqrt($input);
?>

Can anyone help me on where i would go from here? It would be much appreciated.

James

EDIT:

Right im a little bit further on thanks to nick from vbnova. To get it to 0 decimal places ( i dont want to round the numbers up / down) ive got this code:

$number = 1.953434;
$number = str_split($number);

$number = $number[0];

But i really dont know how to put these two codes together. Can anyone help me out?

I think Nick is leading you astray, the above code will not work if the final value is more than 1 digit (e.g. sqrt 100). If you want to round down in all cases use floor.

$number = sqrt($input);
$number = floor($number);
Reply With Quote
  #3  
Old 07-14-2007, 12:25 AM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't really follow what you want, can you be more specific

i.e. what should be displayed for these examples ;

0.5
1.234
1.78
12.432
27.876
123.45
4567.89
Reply With Quote
  #4  
Old 07-15-2007, 10:09 AM
aceofspades's Avatar
aceofspades aceofspades is offline
 
Join Date: Apr 2006
Posts: 306
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi Paul, sorry if i didnt explain myself well. For the examples i would like the following displayed (if we are taking your examples as the result of the square root before they are put to 0 DP):

0.5 ----> 0
1.234 ----> 1
1.78 ----> 1
12.432 ----> 12
27.876 ----> 27
123.45 ----> 123
4567.89 ----> 4567
Reply With Quote
  #5  
Old 07-15-2007, 11:24 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Create a plugin at the hook postbit_display_complete. Add this PHP code:
PHP Code:
$sqrt floor(sqrt($input)); 
Now you can use $sqrt in your postbit(_legacy) template.
Reply With Quote
  #6  
Old 07-15-2007, 01:55 PM
King Kovifor's Avatar
King Kovifor King Kovifor is offline
 
Join Date: Nov 2004
Location: PA
Posts: 3,872
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Your PHP of:

sqrt($input); should return the float of the number; meaning I cannot see anything wrong with it.

If you read PHP's sqrt() page several members have offered alternatives if you would like to try them.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:15 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03849 seconds
  • Memory Usage 2,210KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete